2
def f(num):
    return num

number = f(13195)
list = [ x for x in range(number) if ((x!= 0 and x!=1) and number%x ==0)] #x (the multiplication factor) should not be 0 or 1
max = max(list)
for num in range(1,max) :
    if all(num%i!=0 for i in range(2,num)):
       if num in list:
           print num

Hello, this code is used to find the biggest prime of a number. i have tried using f(13195) and i found the correct numbers, but I got an error of too many items in range, is there any other way of solving this?

But when I typed 600851475143,it says the range has too many items, however, does it mean I should not use range? Thanks in advance !

el psy Congroo
  • 354
  • 1
  • 4
  • 19

2 Answers2

2

You can use xrange(number) instead.

However the loop will take a very long time for large numbers

Alternatively use Python3 where range behaves like xrange

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0
import math
def f(num):
    return num

number = f(600851475143)
list = [ x for x in xrange(int(math.sqrt(number))) if ((x!= 0 and x!=1) and number%x ==0)] 
max = max(list)
for num in xrange(1,max) :
    if all(num%i!=0 for i in xrange(2,num)):
       if num in list: # if sth is true it runs...
           print num

I figured it out, we need to change the range up to square-root of the number, I changed the algorithm rather than trying to find some range bigger than xrange in python

Please find detail explanation here: If you are interested:) "second reply" Why do we check up to the square root of a prime number to determine if it is prime?

Community
  • 1
  • 1
el psy Congroo
  • 354
  • 1
  • 4
  • 19