Project running slowly even with a warp block

Hello. My prime number checker project seems to be running quite slowly even though I put it in a warp block. But still, it doesn't work. Can someone please tell me why? Here is the link to the project: https://snap.berkeley.edu/project?user=feketetcool&project=Prime%20Number%20Checker
If you enter a large number (and it's prime, say, 42643801, and yes I know this is an extremely large prime number and I got it off the internet) it still takes such a long time to check the prime number. I don't know if it is a bug in my script, or the software, I just don't know. Thanks. :slight_smile: :grin:

It's running slowly because your program structure is way too complicated and doesn't take advantage of the fast tools Snap! provides for numeric computation:

Prime Number Checker script pic (1)

Prime Number Checker script pic (2)

This version saves a little time by looking for factors only up to the square root of the candidate prime, but the main thing is that the KEEP higher order function is very, very fast. Look it up in the Reference Manual.

Thanks bh, so shall I just put in a block that you showed to make it quicker? :slight_smile:

Only if you understand how it works! And get in the habit of thinking functionally for future challenges.

Thanks!

The most important part is that there is no need to repeat the calculations 20 million times (20 x 10^6). Just sqrt( 40 x 10^6) only ~7 thousand iterations.

Y?


Edit:I replied to the wrong post

It's not my solution! Brian shows this but unfortunately, put too much attention on the use of list primitives instead of iterations count.

here is my solution:


It accepts a list.

You can just do
[scratchblocks]
<condition...::grey >
[/scratchblocks]
instead of

[scratchblocks]
(if <condition...::grey > then <<true(::grey)::operators>::operators> else <<(::grey)false>::operators>::control
[/scratchblocks]

oh right

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.