Prime Factor Program
问题这就是我要解决的问题。
The prime factors of 13195 are 5, 7, 13, 29. What is the largest prime factor of the number 600851475143?
号
1 2 3 4 5 6 7 8 9 10 11 12 13 | def prime_calc(): num = raw_input("What is the number you want the primes for?") prim_num = [] x = 2 while num/x > 1: new_num = num / x if num % x == 0: return prim_num.append(x) elif num % x != 0: new_num = num/x += 1 return prim_num.append(x) else: break |
我一直收到一个无效的语法错误,从下到第四行,不喜欢我的"+="运算符
此行:
1 | new_num = num/x += 1 |
应该分成两行:
1 2 | x += 1 new_num = num/x |
号
python中的语句
同样,以下两种情况:
1 | return prim_num.append(x) |
也不起作用,因为声明:
1 2 | prime_num.append(x) return prime_num |
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | num=int(input("Please enter number to calculate prime factor")) k=0 item=[] if(num%2==0): prime=2 item.insert(k,prime) k=k+1 j=3 flag=int(num/2) while(j<flag and num>2): if num%j==0: prime=j item.insert(k,prime) k=k+1 num=num/j j=j+2 else: j=j+2 if k==0: print("sorry no prime factor for this number") else: print("Please find the largest prime factor below") print(item.pop()) |