1

In Python2.7 I would like to generate finite sequences for a given integer by repeating the sum of prime factors (sopfr) function over the result(s) until it reaches a prime. The following code for sopfr(n) comes from OEIS A001414.

from sympy import factorint
def sopfr(n):
    return sum(p*e for p, e in factorint(n).items())

>>>sopfr(888)
46
>>>

I'd like to alter this code so that it will give this result

>>>sopfrs(888)
46 25 10 7
>>>

Where 46 = sopfr(888), 25 = sopfr(46)... and so on, until this terminates at a prime, in this case 7. I've read some on and experimented with while and for loops without luck. Experiments that, I'm sure would be good for a laugh. Forgive the novice nature of this question, any help would be appreciated.

Nick Ward
  • 13
  • 4
  • feels like sopfrs could look like: if n is prime (sopfr(n)) else do a block of printing sopfr(n) and calling sopfrs(sopfr(n)) – Jeremy Kahan Jun 13 '17 at 02:34

1 Answers1

0

You can use something like this example:

from sympy import factorint

def sopfr(n):
    sub = []
    while True:
        a = sum(k*v for k,v in factorint(n).items())
        if a == n:
            break
        else:
            sub.append(a)
            n = a
    return sub
# Tests
a = sopfr(888)
print(a)
a = sopfr(65)
print(a)

Output:

[46, 25, 10, 7]
[18, 8, 6, 5]
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43