| Bytes | Lang | Time | Link |
|---|---|---|---|
| 005 | Japt h | 250101T121247Z | Shaggy |
| 008 | J | 250101T030650Z | south |
| 009 | Uiua | 241231T215346Z | nyxbird |
| 101 | Java 8 | 171004T121506Z | Kevin Cr |
| 033 | Factor | 210507T083053Z | chunes |
| 061 | Haskell | 171004T110640Z | Laikoni |
| 004 | Ohm v2 | 171004T132350Z | Cinaski |
| 063 | Javascript ES6 | 171004T175140Z | Endenite |
| 063 | Ruby | 171004T164722Z | Snack |
| 084 | Python 2 | 171004T141459Z | ovs |
| 030 | Retina | 171004T110155Z | Martin E |
| 124 | PowerShell | 171004T132904Z | AdmBorkB |
| 053 | R + pracma | 171004T131409Z | Giuseppe |
| 035 | Mathematica | 171004T104440Z | user2027 |
| 003 | Pyth | 171004T113348Z | Erik the |
| 003 | 05AB1E | 171004T110423Z | scottine |
| 006 | MATL | 171004T104403Z | Luis Men |
| 005 | Jelly | 171004T110231Z | the defa |
| 004 | Husk | 171004T110932Z | Laikoni |
| 006 | Jelly | 171004T110840Z | Sherlock |
| 007 | Actually | 171004T105303Z | user4594 |
Japt -h, 5 bytes
Æ=k x
Æ=k x :Implicit input of integer U
Æ :Map the range [0,U)
= : Reassign to U
k : Prime factors
x : Sum
:Implicit output of last element
Japt, 7 bytes
Works for input 1.
@=k x}g
Java 8, 175 144 142 141 139 101 bytes
n->{for(int i=n;i-->0;)n=f(n,2);return n;};int f(int n,int d){return n>1?n%d<1?f(n/d,2)+d:f(n,d+1):0;}
-38 bytes porting @ovs' Python answer, so make sure to upvote him as well!
n->{ // Method with integer as both parameter and return-type
for(int i=n;i-->0;)// Loop the input `n` amount of times:
n=f(n,2); // Change `n` that many times by calling the recursive method
// with n,2 as parameters
return n;} // Then return the potentially modified `n` as result
int f(int n,int d){ // Separated method with 2 integer parameters & integer return
// (`d` is 2 when we initially call this recursive-method)
return n>1? // If input `n` is larger than 1:
n%d<1? // If `n` is divisible by `d`:
f(n/d // Divide `n` by `d`
,2) // Do a recursive call with n/d,2 as parameters
+d // And add `d` to its result
: // Else:
f(n,d+1) // Do a recursive-call with the next `d` by using n,d+1 as
// parameters
: // Else:
0;} // Simply return 0
Original 175 144 142 141 139 bytes answer:
n->{for(int i,t=n,x;;n=t){for(i=2;i<t;)t=t%i++<1?0:t;if(t>1|n<5)return n;for(t=0,i=1;i++<n;)for(;n%i<1;t+=x)for(n/=x=i;x>9;x/=10)t+=x%10;}}
-1 byte thanks to @Nevay
-2 bytes thanks to @ceilingcat
Explanation:
n->{ // Method with integer as both parameter and return-type
for(int i, // Index-integer `i`
t=n, // Temp integer `t`, starting at the input `n`
x; // Temp integer `x`
; // Loop indefinitely:
n=t){ // After every iteration, replace `n` with the value `t`
for(i=2;i<t;) // Inner loop `i` in the range [2,t):
t=t%i++<1? // If `t` is divisible by `i`:
0 // Set `t` to 0
: // Else:
t; // Leave `t` the same
if(t>1 // If `t` is not 0 or 1 (it means it's a prime),
|n<5) // or if `n` is below 5 (for edge-cases n=1 and n=4)
return n; // Return `n` as result
for(t=0, // Reset `t` to 0
i=1; // Reset `i` to 1
i++<n;) // Inner loop `i` in the range (2,n]:
for(;n%i<1; // Inner loop as long as `n` is divisible by `i`:
t+=x) // After every iteration: Increase `t` by `x`
for(n/=x=i; // Reset `x` to `i`
// And divide `n` by `i`
x>9; // Inner loop as long as `x` contains more than 1 digit:
x/=10) // After every iteration, remove the trailing digit of `x`
t+=x%10;}} // Increase `t` with the trailing digit of `x`
Factor, 33 bytes
[ [ factors Σ ] to-fixed-point ]
to-fixed-pointApply a quotation to an object until it stops changing.factors ΣSum of prime factors.
Haskell, 61 bytes
import Data.Numbers.Primes
until=<<((==)=<<)$sum.primeFactors
Explanation
until=<<((==)=<<) takes a function f and applies it to input x until a fix point is reached, that is f x equals x. 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 be until(\x->f x==x)f, because until 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 as until(\x->(==)(f x)x)f, and as it holds that g (h x) x is the same as (g=<<h)x, we get until(\x->((==)=<<f)x)f. After Eta conversion, this becomes until((==)=<<f)f. But if we now treat (==)=<< as a function which is applied to f, we can see that until(((==)=<<)f)f is again of the form g (h x) x, with g=until, h=((==)=<<) and x=f, so it can be rewritten to (until=<<((==)=<<))f. Using the $ operator to get rid of the outer parentheses and substituting f with sum.primeFactors yields the solution from above.
Ohm v2, 4 bytes
·ΘoΣ
Explanation:
·Θ evaluate the block until the result returned has already been seen
Σ sum
o the full prime factorization
Javascript (ES6), 63 bytes
f=n=>(q=(p=(m,x)=>m<x?0:m%x?p(m,x+1):x+p(m/x,x))(n,2))^n?f(q):q
<input id=i type=number min=0 value=0 oninput="o.innerText=f(i.value)">
<p id=o></p>
Ungolfed:
f=n=>( // Recursive function `f`
p=(m,x=2)=>( // Recursive function `p`, used to sum prime factors
m<x? // If m (the number to be factored) is less than x (the current
0 // iteration), return 0
:m%x? // Else if m doesn't divide x
p(m,x+1) // run the next iteration
: // Else (if m divides x)
x+p(m/x,x) // Divide m by x and repeat the current iteration
),
q=p(n), // Set q to the sum of the prime factors of n
q^n? // If q != n then
f(q) // repeat f with q
: // else
q // return q
)
Ruby, 63 bytes
->n{n.times{n=n.prime_division.map{|x|x.reduce:*}.sum};n}
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.
Python 2, 84 bytes
f=lambda n,d=2:n>1and(n%d and f(n,d+1)or d+f(n/d))
i=input()
exec'i=f(i);'*i
print i
Retina, 30 bytes
{+`(\1|\b11+?\B)+$
$1;$#1$*
;
Input and output in unary.
Try it online! (Performs decimal/unary conversion for convenience.)
Explanation
{+`(\1|\b11+?\B)+$
$1;$#1$*
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 of 1s 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, group 1 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 two 1s 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 group 1 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 of 1, where $#1 is the number of captures made by group 1).
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.
PowerShell, 124 bytes
function f($a){for($i=2;$a-gt1){if(!($a%$i)){$i;$a/=$i}else{$i++}}}
for($x=$args[0];$l-ne$x){$l=$x;$x=(f($x))-join'+'|iex}$x
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, then for loop until $l is -notequal 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 with f($x), -join those together with + and |iex them (short for Invoke-Expression and similar to eval). 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 + pracma, 53 bytes
function(n){for(i in 1:n)n=sum(pracma::factors(n))
n}
R doesn't have a prime factors builtin, but numerous packages (pracma, numbers, etc.) do, so I picked a conveniently short one.
Mathematica, 35 bytes
#//.x_:>Tr[1##&@@@FactorInteger@x]&
(Mathics does not support Tr. I have to implement it manually)
Pyth, 3 bytes
usP
Explanation:
usPGQ The trailing GQ is implicit
PG Get prime factors
s Sum
u Q Repeat until returned value no longer unique starting with the input
05AB1E, 3 bytes
FÒO
Explanations:
FÒO
F Loops <input> times + 1
Ò List of prime factors w/ duplicates
O Total sum of the list
-- implicit output
MATL, 6 bytes
Uses scottinet's idea of looping more times than needed. Thanks also to Shaggy for a pointing out a mistake, now corrected.
t:"Yfs
Explanation
t % Take input (implicit). Duplicate
:" % Do the following that many times
Yf % Array of prime factors
s % Sum of array
% End (implicit). Display (implicit)
Husk, 4 bytes
ω(Σp
Explanation:
ω( -- apply the following function until the result no longer changes (fixpoint)
Σ -- sum
p -- the list of primefactors
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.
ÆfSµÐL
Actually, 7 bytes
⌠w♂πΣ⌡Y
Explanation:
⌠w♂πΣ⌡Y
⌠ ⌡Y do this until output stops changing (fixed-point combinator)
w factor into [prime, exponent] pairs
♂π product of each pair
Σ sum of products