Hi, I need helping with coming up with the most efficient algorithm to check if a number is a prime number
This code I am pretty sure that it works.Code:def is_prime(n): n*=1.0 if (n%2==0 and n!=2 or n%3==0 and n!=3) or n<2: return False for b in range(1,int((n**0.5+1)/6.0+1)): if n%(6*b-1)==0: return False if n %(6*b+1)==0: return False return True def prime(max_num, row, start): r_check = 0 my_file = open("datafile.txt", "w") for num in range(start,max_num+1): r_check += 1 if r_check > row: my_file.write("\n") r_check = 1 if is_prime(num): my_file.write("1") else: my_file.write("0") my_file.close()
This is a code i found on the internet, it puts in a file, 0 for not prime and 1 for prime. Does anyone know of a faster algorithm? or a way to simplify it?
I'm not asking for code here, but I guess just the algorithm written in steps.
But if you could write the code, that would be awesome.
Thanks
Also whats the pattern for prime numbers?
If you check this out
The image is compressed, its actually 900x900, so if you want to see a clearer image, save the image, then zoom in.
This is an animation i made, each frame is a square grid, where each blue pixel is a prime and black pixel isnt prime.
each frame gets a bigger grid, this is because the first grid
is a
1x1 square, then
4x4 square, then
9x9
16x16
25x25
36x36
49x49
.
.
.
.
900x900
and each grid starts from the top left.
the grid is like this
1, 2, 3, 4, 5, 6, 7,..., n
n+1 ...................... 2n
2n+1.......................
3n+1.......................
...............................
.................................
...................................
n(n-1)+1 ................n^2
in the animation's case, n is 1, 2, 3, 4, ..., 30
for each number, if its a prime, its represented as a blue pixel, otherwise its a black pixel.
Does anyone see a pattern?


LinkBack URL
About LinkBacks
