Zainspirowany pierwiastkami cyfrowymi, główny faktorowy pierwiastek z liczby to liczba, która pojawia się, gdy weźmiesz czynniki pierwsze liczby, zsumujesz je i powtórzysz proces na wynikowej liczbie, kontynuując aż do uzyskania liczby pierwszej ( który ma sam w sobie jedyny główny czynnik, a zatem jest swoim głównym pierwiastkiem faktorowym). Pierwotny pierwiastek faktoralny z 4 wynosi 4, ponieważ 2 * 2 = 2 + 2, i jest to jedyny pierwszoplanowy pierwiastkowy pierwiastek z liczby całkowitej większej niż 1 (co jest innym szczególnym przypadkiem, ponieważ nie ma żadnych czynników pierwszych). Sekwencja OEIS utworzona z głównych pierwiastków faktoralnych to A029908 .
Na przykład główny faktoralny rdzeń 24 to:
24=2*2*2*3
2+2+2+3=9=3*3
3+3=6=2*3
2+3=5, and the only prime factor of 5 is 5. Therefore, the prime factoral root of 24 is 5.
Twoje zadanie:
Napisz program lub funkcję, która znajdzie główny faktoralny rdzeń wejściowej liczby całkowitej.
Wejście:
Liczba całkowita, wprowadzana dowolną rozsądną metodą, od 2 do największej liczby całkowitej obsługiwanej przez Twój język (włącznie). W szczególności wybór języka, który ma nieproporcjonalnie niski maksymalny rozmiar liczby całkowitej, jest niedozwolony (a także narusza tę standardową lukę )
Wynik:
Liczba całkowita, główny faktorowy pierwiastek wejściowy.
Przypadki testowe:
4 -> 4
24 -> 5
11 -> 11
250 -> 17
Punktacja:
To jest golf golfowy , najniższy wynik w bajtach wygrywa!
4
do przypadków testowych, ponieważ jest to wyjątek i łatwo o nim zapomnieć podczas testowania odpowiedzi?Odpowiedzi:
05AB1E, 3 bytes
Try it online!
Explanations:
źródło
4
.Haskell, 61 bytes
Try it online!
Explanation
until=<<((==)=<<)
takes a functionf
and applies it to inputx
until a fix point is reached, that isf x
equalsx
.primeFactors
returns the list of prime factors of a number,sum
yields the sum of a list of numbers.But wait, why does
until=<<((==)=<<)
the job and looks so weird?If we assume
f=sum.primeFactors
, a more natural definition would beuntil(\x->f x==x)f
, becauseuntil
takes a predicate (a function which returns a boolean), a function which has the same input and return type (e.g.Int -> Int
) and value of this type, and then applies the function to the value until the predicate is fulfilled.until(\x->f x==x)f
is the same asuntil(\x->(==)(f x)x)f
, and as it holds thatg (h x) x
is the same as(g=<<h)x
, we getuntil(\x->((==)=<<f)x)f
. After Eta conversion, this becomesuntil((==)=<<f)f
. But if we now treat(==)=<<
as a function which is applied tof
, we can see thatuntil(((==)=<<)f)f
is again of the formg (h x) x
, withg=until
,h=((==)=<<)
andx=f
, so it can be rewritten to(until=<<((==)=<<))f
. Using the$
operator to get rid of the outer parentheses and substitutingf
withsum.primeFactors
yields the solution from above.źródło
=<<((==)=<<)$
Whaaaaaat.Husk, 4 bytes
Try it online!
Explanation:
źródło
Pyth, 3 bytes
Try it here.
Explanation:
źródło
The trailing GQ is implicit
Python 2, 84 bytes
Try it online!
źródło
f=lambda n,d=2:n>1and(n%d and f(n,d+1)or d+f(n/d))
work? I've never programmed in Python (mainly Java and C#), so I'm unsure what the result of this function is. Does this function modify the inputn
and return it afterwards, or is it similar to a boolean wheren>1and(n%d and f(n,d+1)or d+f(n/d))
is either 0 or 1, or 0 orn
, or something else? I'm trying to visualize how a port of this would look like in Java/C#, but I'm unable to because I don't really understand Python lambdas like this in general.n>1 ? (n%d!=0 ? f(n, d+1) : d+f(n/d)) : n>1
. In general termsx and y
is equivalent tox ? y : x
.x and y or z
is equivalent tox ? y : z
in most cases.f=(n,d=2)->n>1?n%d>0?f(n,d+1):d+f(n/d):0
.x and y
beingx ? y : x
from JavaScript as well. Thanks!Java 8,
175144142141 bytes-1 byte thanks to @Nevay.
Unlike single bytes in some of the golfing languages, Java is pretty verbose for prime-checks, prime-factors, digit-sums, and such, so I guess less than 200 isn't too shabby.
Can most likely still be golfed by combining loops
and not using a separated recursive method for the digit sum.Explanation:
Try it here.
źródło
i,t=n,x
looks like it belongs in Python, hahaint
(unlike Python). ;)i++<n
instead of++i<=n
.Jelly, 5 bytes
Explanations:
Try it online!
źródło
Retina, 30 bytes
Input and output in unary.
Try it online! (Performs decimal/unary conversion for convenience.)
Explanation
The
{
instructs Retina to run the entire program in a loop until a full pass fails to modify the string, i.e. until a fixed point is reached. Consequently, the program itself computes one step of summing the prime factors of the current value.This stage itself computes the prime factorisation of the input. The
+
is similar to{
but loops only this stage until it stops changing the string. The regex tries to match the final run of1
s by repeatedly matching the same substring (i.e. the factor). The way this is done is a bit convoluted due to the forward reference\1
. On the first iteration, group1
hasn't captured anything yet, so\1
fails unconditionally. Instead, we have to match\b11+?\B
which is the smallest possible substring that starts at the beginning of the run, contains at least two1
s and doesn't cover the entire run. Subsequent iterations will not be able to use this alternative again, due to the\b
. So on all further iterations, we're matching\1
, i.e. the same substring over and over again. This process has to hit the end of the string exactly ($
) to make sure we've captured and actual divisor. The benefit of using this somewhat tricky approach is that group1
will have been used exactly n/d times, i.e. what remains after dividing out the divisor d.We replace this match with d (
$1
), a separating;
and n/d ($#1$*
, which inserts$#1
copies of1
, where$#1
is the number of captures made by group1
).This process stops once the final run in the string is itself a prime, because then the regex no longer matches.
All we need to do to sum the primes is to remove all the separators.
źródło
Ohm v2, 4 bytes
Try it online!
Explanation:
źródło
Actually, 7 bytes
Try it online!
Explanation:
źródło
R + pracma, 53 bytes
Try it online! (r-fiddle)
R doesn't have a prime factors builtin, but numerous packages (
pracma
,numbers
, etc.) do, so I picked a conveniently short one.źródło
Jelly, 6 bytes
This answer uses one of Jelly's many prime factorization builtins, and the quick for
repeat until the results are no longer unique
.Try it online!
źródło
1
is the only case where the number of steps needed is equal ton
(which is fine; with1
we only need to run it once), and there don't seem to be any cases where the number of steps is greater thann
(i.e. there don't seem to be any counterexamples). Ah well, I've been outgolfed :DMATL, 6 bytes
Uses scottinet's idea of looping more times than needed. Thanks also to Shaggy for a pointing out a mistake, now corrected.
Try it online!
Explanation
źródło
4
.PowerShell, 124 bytes
Try it online!
PowerShell doesn't have any prime factorization built-ins, so this uses code from my answer on Prime Factors Buddies (the top line) to perform the factorization calculations.
The second line is the meat of this program. We take input from
$args
into$x
, thenfor
loop until$l
is-n
ote
qual to$x
. (The first iteration,$l
is$null
and$x
is an integer, so we'll loop at least once).Inside the loop, we set our
$l = $x
to determine if we've hit the end of the loop or not. Then we get the factors of$x
withf($x)
,-join
those together with+
and|iex
them (short forInvoke-Expression
and similar toeval
). That's stored back into$x
. Thus, we've hit the "end" where the prime factorization summed together is back to itself. Then, we simply place$x
on the pipeline and output is implicit.źródło
Mathematica, 35 bytes
Try it online!
(Mathics does not support
Tr
. I have to implement it manually)źródło
1##&
is short forTimes
andFixedPoint
can almost always be shortened with//.
:#//.x_:>Tr[1##&@@@FactorInteger@x]&
Times
, but I've not known about theFixedPoint
trick.Tr
to Mathics a while back.Ruby, 63 bytes
Try it online!
Uses the
-rprime
flag for +6 bytes to make use of Prime#prime_division.prime_division
returns pairs of[prime, exponent]
(for example, for 24 we have the factors[2, 2, 2, 3]
so it gives[[2, 3], [3, 1]]
) so in each step we just multiply the members of those pairs together and sum the results.źródło
Javascript (ES6), 63 bytes
Ungolfed:
źródło
Java 8, 101 bytes
Port of @ovs's amazing Python 2 answer.
Explanation:
Try it here.
źródło