I'm new with Python and i want to know if is there any way to improve the time execution of this simple code:

- Code: Select all
`import math`

def IsPrime(x):

if x == 1:

return False

elif x == 2 or x == 3 or x == 5:

return True

elif x % 2 == 0 or x % 3 == 0 or x % 5 == 0:

return False

top = int(math.ceil(math.sqrt(x)))

for i in range(7, top+1, 30):

if x%i == 0 or x%(i+2) == 0 or x%(i+4) == 0 or x%(i+6) == 0 or x%(i+10) == 0 or x%(i+12) == 0 or x%(i+16) == 0 or x%(i+22) == 0 or x%(i+24) == 0:

return False

return True

print IsPrime(9007199254740881)

when i run it, it takes at least 6 seconds to return me the result, i've coded the same task in other languages like Javascript and it takes less than 1 second

- Code: Select all
`<script>`

function IsPrime(x)

{

if (x == 1)

return false;

else if( x == 2 || x == 3 || x == 5)

return true;

else if( x % 2 == 0 || x % 3 == 0 || x % 5 == 0)

return false;

top = Math.ceil(Math.sqrt(x));

for(i=7; i < top+1; i+=30)

{

if( x%i == 0 || x%(i+2) == 0 || x%(i+4) == 0 || x%(i+6) == 0 || x%(i+10) == 0 || x%(i+12) == 0 || x%(i+16) == 0 || x%(i+22) == 0 || x%(i+24) == 0)

return false;

}

return true;

}

window.alert( IsPrime(9007199254740881) );

</script>

As i'm new i don't know if i'm loosing something.

Tanks in advance