File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Prime Spiral
2
+
3
+ ## Some useful facts:
4
+
5
+ - All primes except 2 are `odd`.
6
+ - All primes `greater` than 3 can be written in the form `6k - 1` or `6k + 1`.
7
+
8
+ Using those facts we can optimaze isPrime function to check only ** _ numbers 6k - 1 and 6k + 1_ **
9
+ instead of ~~ all the numbers between 1 and the given number~~ .
10
+
11
+ source :[ Project_Euler] ( https://projecteuler.net/overview=007 )
12
+
13
+ ``` javascript
14
+ // Function to test if number is prime in P5.js
15
+ function isPrime (value ) {
16
+ if (value < 2 ) return false ;
17
+ else if (value < 4 ) return true ;
18
+ else if (value % 2 == 0 || value % 3 == 0 ) return false ;
19
+ else {
20
+ for (let i = 5 ; i <= sqrt (value); i += 6 ) {
21
+ if (value % i == 0 || value % (i + 2 ) == 0 ) {
22
+ return false ;
23
+ }
24
+ }
25
+ }
26
+
27
+ return true ;
28
+ }
29
+ ```
30
+
31
+ Thank you to @MoulatiMehdi for the above note!
You can’t perform that action at this time.
0 commit comments