Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Practical Number?

Bob Johnsonson
Greenhorn
Posts: 2
The Wikipedia article is helpful, I think.
Use some integer factorization algorithm to factor the number you want, getting each prime factor and the exponent it has in the prime factorization.
Store the prime factors in an sorted list [p1,p2,p3,...,pj] and exponent [a1,a2,a3,...,aj].
Set an integer sigma to 1.
If the first prime factor is not 2, the number is not practical.
Then for each prime factor:
Multiply sigma by (p_i^a_i-1)/(p_i-1)
if sigma+1 is greater than the next largest prime factor, your number is not practical.
Else, increment i and continue again.
If that holds for every prime factor, then your number is indeed practical.

That method, assuming a decent factorization algorithm is used, would work better than the method described in the previous post, I think.

Campbell Ritchie
Sheriff
Posts: 48445
56
If I see James tonight, I shall show him your algorithm and ask him to explain it.

Campbell Ritchie
Sheriff
Posts: 48445
56
And welcome to JavaRanch Bob Johnsonson

Bob Johnsonson
Greenhorn
Posts: 2
I'm not sure I could explain why that works, but it just takes what Wikipedia says about the condition of how the ith prime factor is less than or equal to the sum of the divisors of the first i-1 factors (raised to their exponent)+1. Since the divisor function is multiplicative, i.e. σ(a*b)=σ(a)σ(b) if a and b are relatively prime, one doesn't need to compute the whole thing each time, you can just multiply by σ(p_i-1^a_i-1) each iteration, saving a bit of calculation.

Campbell Ritchie
Sheriff
Posts: 48445
56
I am afraid James is likely to be too busy to help until about Wednesday or Thursday, but I passed a message to him yesterday.

I did say
. . .