A simple algorithm to calculate prime numbers! In theory, the algorithm I am using could calculate all prime numbers, but the scratch lists are limited to 200.000 - And also feel free to look inside, remix and reuse!
The definition of a prime number is 'a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1' The script adds the numbers 2, 3 initially, then it starts calculating the rest, by using two counters. One counter is the number that we want to check for being a prime number. The second counter resets for every new number we want to check and counts up to the first counter from the value 3. We keep dividing the first counter by the second counter, to check whether the result is a whole number. If a whole number appears, it is NOT a prime number. Implemented in scratch, it look like this: If length of (counter 1 divided by counter 2) is greater than length of (counter 1), it is NOT a prime number! We use the length() function because if the operation results in a decimal, which is always longer than the previous integer (whole number), we can conclude that it cannot be exactly divided by counter 2. Example for counter 1 = 10 10/3 = 3.333... (decimal) 10/4 = 2.5 (decimal) 10/5 = 2 (whole number) --> NOT a prime number! Example for counter 1 = 7 7/3 = 2.333... (decimal) 7/4 = 1.75 (decimal) 7/5 = 1.4 (decimal) 7/6 = 1,167 (decimal) 7 cannot be divided exactly by whole numbers smaller than seven, so 7 IS a prime number! Hope you enjoy this little project!