-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1dec22OddPrimes.js
66 lines (56 loc) · 2.11 KB
/
1dec22OddPrimes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Description:
Primes that have only odd digits are pure odd digits primes, obvious but necessary definition. Examples of pure odd digit primes are: 11, 13, 17, 19, 31... If a prime has only one even digit does not belong to pure odd digits prime, no matter the amount of odd digits that may have.
Create a function, only_oddDigPrimes(), that receive any positive integer n, and output a list like the one below:
[number pure odd digit primes below n, largest pure odd digit prime smaller than n, smallest pure odd digit prime higher than n]
Let's see some cases:
only_oddDigPrimes(20) ----> [7, 19, 31]
///7, beacause we have seven pure odd digit primes below 20 and are 3, 5, 7, 11, 13, 17, 19
19, because is the nearest prime of this type to 20
31, is the first pure odd digit that we encounter after 20///
only_oddDigPrimes(40) ----> [9, 37, 53]
In the case that n, the given value, is a pure odd prime, should be counted with the found primes and search for the immediately below and the immediately after.
Happy coding!!
const pureOdd = n => !/[02468]/.test(n);
const getPrimes = n => {
const primes = [];
for (let i = 2; i <= n; i++) {
if (isPrime(i) && pureOdd(i)) primes.push(i);
}
return primes;
}
const isPrime = n => {
for (let i = 2, s = Math.sqrt(n); i <= s; i++)
if (n % i === 0) return false;
return n > 1;
}
const nextPrime = n => {
while(n++) if (isPrime(n) && pureOdd(n)) return n;
}
const onlyOddDigPrimes = n => {
const primes = getPrimes(n);
return [primes.length, primes.pop(), nextPrime(n)];
}
//----------------Repracticing the above-----------------------------//
const odd = n => !/[24680]/.test(n)
const isPrime = n => {
for(let i = 2; s = Math.sqrt(n); i <= s; i++){
if(n % i === 0) return false
return n > 1;
}
}
const nextPrimeee = n => {
while(n++) if(isPrime(n) && pureOdd(n)) return n
}
const one = n => {
const primes = getPrimes(n)
}
const getttprimes = n =>{
const primes = []
for(let if = 2; i <=n; i++){
if(is)
}
}
const onlyOddDigPrimes = n => {
const primes = getPrimes(n)
return [primes.length, primes.pop(), nextPrimeee(n)]
}