Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Biggest Prime Number
- // Description
- // 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.
- // Input
- // On the first line you will receive the number N
- // Output
- // Print the biggest prime number which is <= N
- // Constraints
- // 2 <= N <= 10 000 000
- // Sample tests
- // Input Output
- // 13 13
- // 126 113
- // 26 23
- const input = ['10000000'];
- const print = this.print || console.log;
- const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- let numberToCheck = +gets();
- function isPrime (n) {
- if (n === 1) {
- return false;
- } else if (n === 2) {
- return true;
- } else {
- for (let i = 2; i < n; i++) {
- if (n % i === 0) {
- return false;
- }
- }
- return true;
- }
- }
- while (isPrime(numberToCheck) === false) {
- numberToCheck--;
- }
- print(numberToCheck);
Advertisement
Add Comment
Please, Sign In to add comment