Python Forum

Full Version: a weired problem after restructing algorithm
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,Those days I try to restructing algorithm of a Prime module,then I met a weired problem
whem I try to use a function(speedUp_Prime) to replace
if i > Number//3:#speedUp_Prime is slower then this,they are same but why this is faster?
return True
the runingn time would increase a lot,on my laptop it was about 9 seconds,after replace with speedUp_Prime the time is about 14 seconds.almost increased 50% that is horrible,and so weired,so I wrote a new fucntion is_even,but nothing change with is_even.
my IDE is Eclipse IDE for Java Developers
Version: Neon.3 Release (4.6.3)
Build id: 20170314-1500
Python version:3.6.5
windows 10
# -*- coding: UTF-8 -*-

def get_Prime_Fast(MinNum,MaxNum):#get all Prime in Range(MinNum,MaxNum),print primes and counting
    n=1    
    for i in range(MinNum,MaxNum):
       if is_Prime(i):
           print(n,i)
           n += 1

def is_Prime(Number):#Number is a Prime or not,return boolean
    if Number==2 or Number ==5:# 2 and 5 are Prime
        return True 
    if is_even(Number) or Number%10 ==5 :#Prime Not Even and last Number Not 5
        return False 

    i=0
    for i in range(3,Number,2):#already remove even,so start at 3 step 2
        if(Number%i == 0):
            return False
#          if speedUp_Prime(i,Number):
#              return True
        if i > Number//3:#speedUp_Prime is slower then this,they are same but why this is faster?
                return True   
    else:#After calculate all Number
        return True


def speedUp_Prime(i,Number):#Shall we have to divide all number?
      if i > Number//3:
            return True

def is_even(Number):#Number is Even or not,return boolean
    if Number % 2 ==0:
        return True
    else:
        return False

if __name__=='__main__':
    import time
    start = time.time()
    get_Prime_Fast(2,100000)
    end = time.time()
    print (end-start)