爪哇8,175个 144 142 141字节
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;n/=i,t+=x)for(x=i;x>9;x/=10)t+=x%10;}}
-1个字节感谢@Nevay。
与某些高尔夫球语言中的单字节不同,Java在素数检查,素因数,数字和等方面相当冗长,因此我认为不足200的值还不算太破旧。
仍然有可能通过组合循环而不是对数字总和使用单独的递归方法来打高尔夫球。
说明:
在这里尝试。
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 (1) indefinitely
n=t){ // After every iteration, replace `n` with the value `t`
for(i=2; // Reset `i` to 2
i<t; // Inner loop (2) from 2 to `t` (exclusive)
t=t%i++<1? // If `t` is divisible by `i`:
0 // Set `t` to 0
: // Else:
t // Leave `t` the same
); // End of inner loop (2)
if(t>1 // If `t` is not 0 (it means it's a prime),
|n<5) // or if `n` is below 5 (for edge-cases `4` and 'prime' `1`)
return n; // Return `n` as result
for(t=0, // Reset `t` to 0
i=1; // Reset `i` to 1
i++<n;) // Inner loop (3) from 2 to `n` (inclusive)
for(;n%i<1; // Inner loop (4) as long as `n` is divisible by `i`
n/=i, // After every iteration: Divide `n` by `i`,
t+=x) // and increase `t` by `x`
for(x=i; // Reset `x` to `i`
x>9; // Inner loop (5) as long as `x` contains more than 1 digit
x/=10) // After every iteration, remove the trailing digit
t+=n%10; // Increase `t` with the trailing digit of `n`
// End of inner loop (5) (implicit / single-line body)
// End of inner loop (4) (implicit / single-line body)
// End of inner loop (3) (implicit / single-line body)
} // End of loop (1)
} // End of method
4
测试用例,因为这是一个例外,在测试答案时很容易忘记它?