Python Forum
a weired problem after restructing algorithm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a weired problem after restructing algorithm
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Floor approximation problem in algorithm gradlon93 3 947 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
Big Grin question about simple algorithm to my problem jamie_01 1 1,669 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  python result problem of an iteration algorithm for power allocation Jessica 1 2,622 Sep-07-2018, 08:08 PM
Last Post: micseydel
  Problem in a path finding algorithm (counter is not working) Kowalski 3 3,251 Feb-05-2018, 01:23 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020