Skip to content

Commit 1f3dba6

Browse files
authored
Create README.md
1 parent 16a9469 commit 1f3dba6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

167_Prime_Spiral/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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!

0 commit comments

Comments
 (0)