Advertisement
angryatti

Prime in console.... Maybe Prime Javascript v3

Aug 29th, 2022 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict"
  2.  
  3.  
  4. console.log(maybePrime(20, 50))
  5.  
  6. function isPrime(prm) {
  7.  
  8.     let countPrime = 0
  9.  
  10.     for (let j = 2; j <= Math.floor(Math.sqrt(prm)); j++) {
  11.  
  12.  
  13.  
  14.  
  15.         if (prm % j == 0) {
  16.             countPrime++
  17.  
  18.         }
  19.  
  20.  
  21.  
  22.  
  23.  
  24.     }
  25.  
  26.  
  27.     if (countPrime == 0) {
  28.  
  29.         return true
  30.  
  31.     }
  32.  
  33.     else {
  34.  
  35.         return false
  36.     }
  37.  
  38.  
  39. }
  40.  
  41.  
  42. function maybePrime(r1, r2) {
  43.  
  44.     let countPrime = 0
  45.     let foundPrime = 0
  46.  
  47.     let arr = []
  48.  
  49.     for (let i = r1; i <= r2; i++) {
  50.  
  51.  
  52.  
  53.         let countPrime = 0
  54.         for (let j = 2; j <= Math.floor(Math.sqrt(i)); j++) {
  55.  
  56.  
  57.  
  58.  
  59.             if (i % j == 0) {
  60.                 countPrime++
  61.  
  62.             }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.         }
  69.  
  70.         if (countPrime == 0) {
  71.  
  72.             //console.log(i)
  73.  
  74.             console.log(isPrime(i))
  75.  
  76.             foundPrime++
  77.             arr.push(i)
  78.         }
  79.  
  80.         if (foundPrime > 2) {
  81.             break
  82.  
  83.         }
  84.  
  85.     }
  86.  
  87.     return arr
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement