Discuss Scratch
- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
I was so happy when I tested this and it worked. First 2, then 3, skipped 4 and added 5…
I just need someone to confirm the code and conclude that it is a working Prime Number Generator.
https://scratch.mit.edu/projects/99036385/
P.S.
Ok, I know people have made things like this before. But I have yet to have seen one on Scratch. So I decided to make one, and after hours of debugging, I'm done.
I just need someone to confirm the code and conclude that it is a working Prime Number Generator.
https://scratch.mit.edu/projects/99036385/
P.S.
Ok, I know people have made things like this before. But I have yet to have seen one on Scratch. So I decided to make one, and after hours of debugging, I'm done.
- deck26
-
Scratcher
1000+ posts
Prime Number Generator!?
There certainly are plenty of prime number generators on Scratch - a quick search shows that. Here's mine for example https://scratch.mit.edu/projects/36613800/
Yours does work but can be improved.
Since 2 is the only even prime it makes sense to treat it as a special case and only test odd numbers after that.
Unless the number is a square any factor greater than the square root must have a partner less than the square root so you only need to check up to the square root - this makes a big difference as the numbers get bigger.
If a number isn't divisible by any of the primes you've found so far it is a prime - skip the composite numbers when checking for factors.
Combine these and for a number like 401 you'd only check these numbers as factors - 3, 5, 7, 11, 13, 17, 19 whereas you check all the numbers from 2 to 400.
Yours does work but can be improved.
Since 2 is the only even prime it makes sense to treat it as a special case and only test odd numbers after that.
Unless the number is a square any factor greater than the square root must have a partner less than the square root so you only need to check up to the square root - this makes a big difference as the numbers get bigger.
If a number isn't divisible by any of the primes you've found so far it is a prime - skip the composite numbers when checking for factors.
Combine these and for a number like 401 you'd only check these numbers as factors - 3, 5, 7, 11, 13, 17, 19 whereas you check all the numbers from 2 to 400.
- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
There certainly are plenty of prime number generators on Scratch - a quick search shows that. Here's mine for example https://scratch.mit.edu/projects/36613800/Thank you for the tips, though in the case of 401, I only check up to 200.
Yours does work but can be improved.
Since 2 is the only even prime it makes sense to treat it as a special case and only test odd numbers after that.
Unless the number is a square any factor greater than the square root must have a partner less than the square root so you only need to check up to the square root - this makes a big difference as the numbers get bigger.
If a number isn't divisible by any of the primes you've found so far it is a prime - skip the composite numbers when checking for factors.
Combine these and for a number like 401 you'd only check these numbers as factors - 3, 5, 7, 11, 13, 17, 19 whereas you check all the numbers from 2 to 400.
- deck26
-
Scratcher
1000+ posts
Prime Number Generator!?
Thank you for the tips, though in the case of 401, I only check up to 200.OK, I didn't look that closely. You're still checking 199 potential factors instead of my 7 though.
- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
Yes, you are quite right. I will get to work on that. If it took 5 hours to get my slow generator to work, it's going to take a couple days to refine it to your calibre.Thank you for the tips, though in the case of 401, I only check up to 200.OK, I didn't look that closely. You're still checking 199 potential factors instead of my 7 though.

- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
Feel free to copy any of my coding.Why, thank you. I won't directly copy, but I will try to use the concept of square root factors. Also, could you go into depth about what I should do when the number DOES happen to be square?
- deck26
-
Scratcher
1000+ posts
Prime Number Generator!?
As long as you check up to and including the square root there's no problem.Feel free to copy any of my coding.Why, thank you. I won't directly copy, but I will try to use the concept of square root factors. Also, could you go into depth about what I should do when the number DOES happen to be square?
- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
So, just to sum it up, I round the square root of the number I'm checking, then check all the prime numbers up to, and including, that number to see if it is a composite. If not, I add it to the list, and change the number by 1, and restart the process.As long as you check up to and including the square root there's no problem.Feel free to copy any of my coding.Why, thank you. I won't directly copy, but I will try to use the concept of square root factors. Also, could you go into depth about what I should do when the number DOES happen to be square?
- deck26
-
Scratcher
1000+ posts
Prime Number Generator!?
Pretty much it but no need to round the square number. If it's not a whole number it doesn't affect anything. I'd just put even numbers above 2 straight into the composites though and change the number by 2.So, just to sum it up, I round the square root of the number I'm checking, then check all the prime numbers up to, and including, that number to see if it is a composite. If not, I add it to the list, and change the number by 1, and restart the process.As long as you check up to and including the square root there's no problem.Feel free to copy any of my coding.Why, thank you. I won't directly copy, but I will try to use the concept of square root factors. Also, could you go into depth about what I should do when the number DOES happen to be square?
So store 2 in primes, then repeat the following
1. increase number by 1 and check for prime - store in appropriate list
2. increase number by 1 and store as composite
- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
So, I have taken what you have told me, and some snippets of your code, and now I have created this:Pretty much it but no need to round the square number. If it's not a whole number it doesn't affect anything. I'd just put even numbers above 2 straight into the composites though and change the number by 2.So, just to sum it up, I round the square root of the number I'm checking, then check all the prime numbers up to, and including, that number to see if it is a composite. If not, I add it to the list, and change the number by 1, and restart the process.As long as you check up to and including the square root there's no problem.Feel free to copy any of my coding.Why, thank you. I won't directly copy, but I will try to use the concept of square root factors. Also, could you go into depth about what I should do when the number DOES happen to be square?
So store 2 in primes, then repeat the following
1. increase number by 1 and check for prime - store in appropriate list
2. increase number by 1 and store as composite
https://scratch.mit.edu/projects/99195997/#player
I'm not quite sure if this is the fastest possible, so I was hoping you could check.
- P444
-
Scratcher
500+ posts
Prime Number Generator!?
Here is a faster remix > https://scratch.mit.edu/projects/99197535/
- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
Here is a faster remix > https://scratch.mit.edu/projects/99197535/I literally added a hide list function in mine the moment I got a notification about your faster method

- P444
-
Scratcher
500+ posts
Prime Number Generator!?
Mine is still faster cause I am using the custom block without screen refresh xd
- deck26
-
Scratcher
1000+ posts
Prime Number Generator!?
Mine is still faster cause I am using the custom block without screen refresh xdAlso because you're not showing a timer. (edit - sorry I thought you were comparing yours with mine)
Stripping out the variable updates etc can make it at least twice as fast as yours. So it's a case of priorities really - maximising the prime calculation or showing what's happening.
Last edited by deck26 (Feb. 25, 2016 11:02:44)
- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
So, I could make the generator faster by completely removing the Composite List and all associated blocks?Mine is still faster cause I am using the custom block without screen refresh xdAlso because you're not showing a timer.
Stripping out the variable updates etc can make it at least twice as fast as yours. So it's a case of priorities really - maximising the prime calculation or showing what's happening.
Also, hiding the lists make it faster, so maybe hiding the variables might make it slightly faster?
EDIT: After two hours, my new generator has calculated over 200,000 prime numbers, which is much faster than my previous, which would only calculate 2 or 3 thousand in that time.
Last edited by LastAttack1257 (Feb. 25, 2016 11:01:53)
- deck26
-
Scratcher
1000+ posts
Prime Number Generator!?
If you want to see who can produce the quickest version I suggest setting a time limit - eg 60 seconds. The green flag script should hide any variables that need hiding following a previous run and then reset the timer and you should use a
when [timer v] > (60)I see no need to store composite numbers and displaying lists that are changing is always slow.
stop [other scripts in sprite v]
then show any variables you want to show
- deck26
-
Scratcher
1000+ posts
Prime Number Generator!?
Obviously it depends on the speed of the computer etc but I've just generated around 30,400 primes in 60 seconds by not showing anything changing on the screen..
- LastAttack1257
-
Scratcher
100+ posts
Prime Number Generator!?
Obviously it depends on the speed of the computer etc but I've just generated around 30,400 primes in 60 seconds by not showing anything changing on the screen..I am keeping my generator on for as long as possible to see the highest number I can get to,
but I'll test in another tab later.
- deck26
-
Scratcher
1000+ posts
Prime Number Generator!?
You may hit problems with large numbers after a while though. Off the top of my head I think Scratch will only cope with around 14 digits before it can't store integer values accurately.Obviously it depends on the speed of the computer etc but I've just generated around 30,400 primes in 60 seconds by not showing anything changing on the screen..I am keeping my generator on for as long as possible to see the highest number I can get to,
but I'll test in another tab later.