alboig

BiggestPrime - JavascriptModule0

Apr 14th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Biggest Prime Number
  2. // Description
  3. // Write a program that finds all prime numbers in the range 1 ... N. Use the Sieve of Eratosthenes algorithm. The program should print the biggest prime number which is <= N.
  4. // Input
  5. // On the first line you will receive the number N
  6. // Output
  7. // Print the biggest prime number which is <= N
  8. // Constraints
  9. // 2 <= N <= 10 000 000
  10. // Sample tests
  11. // Input Output
  12. // 13 13
  13. // 126 113
  14. // 26 23
  15.  
  16. const input = ['10000000'];
  17. const print = this.print || console.log;
  18. const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  19. let numberToCheck = +gets();
  20. function isPrime (n) {
  21.   if (n === 1) {
  22.     return false;
  23.   } else if (n === 2) {
  24.     return true;
  25.   } else {
  26.     for (let i = 2; i < n; i++) {
  27.       if (n % i === 0) {
  28.         return false;
  29.       }
  30.     }
  31.     return true;
  32.   }
  33. }
  34. while (isPrime(numberToCheck) === false) {
  35.   numberToCheck--;
  36. }
  37. print(numberToCheck);
Advertisement
Add Comment
Please, Sign In to add comment