Advertisement
mivebe

Ex: 28 - Nested loops

Nov 1st, 2020 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.72 KB | Software | 0 0
  1.  
  2. function solve(maxFirst, maxSecond, maxThird) {
  3.     const minJ = maxSecond < 2 ? 1 : 2
  4.     for (let i = 1; i <= maxFirst; i++) {
  5.         if (i % 2 !== 0) {
  6.             continue;
  7.         }
  8.         for (let j = minJ; j <= Math.min(7, maxSecond); j++) {
  9.             if (!isPrime(j)) {
  10.                 continue;
  11.             }
  12.             for (let k = 1; k <= maxThird; k++) {
  13.                 if (k % 2 !== 0) {
  14.                     continue;
  15.                 }
  16.                 console.log(`${i} ${j} ${k}`)
  17.             }
  18.         }
  19.     }
  20.  
  21.     function isPrime(n) {
  22.         for (let j = 2; j < n; j++) {
  23.             if (n % j === 0) {
  24.                 return false
  25.             }
  26.         }
  27.         return true
  28.     }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement