|
| 1 | +// question 18 |
| 2 | +// create a program that will print the first 100 prime numbers |
| 3 | + |
| 4 | + |
| 5 | +// this is a good question where i have to basically |
| 6 | +//cheack two things and that are first that i have to cheack that that number is a prime number or not |
| 7 | +// second thing that i have to cheak that is that is i got my 100 prime number |
| 8 | + |
| 9 | +// now the funciton for cheacking if my number is a prime number or not |
| 10 | +function isprime(number){ |
| 11 | +if(number<2){ |
| 12 | + return false |
| 13 | +} for(let i=2; i<=Math.sqrt(number);i++){ |
| 14 | + if(number%i==0){ |
| 15 | + return false |
| 16 | + } |
| 17 | +}return true |
| 18 | +} |
| 19 | +// now lets print the prime and cheack wheather i have printed the 100 or not |
| 20 | +function prime (nums){ |
| 21 | + let count=0; |
| 22 | + let num=2 |
| 23 | + while(count<nums){ |
| 24 | + if(isprime(num)){ |
| 25 | + count++ |
| 26 | + console.log(num) |
| 27 | + } |
| 28 | + |
| 29 | + num++ |
| 30 | + } |
| 31 | +} |
| 32 | +prime(100) |
| 33 | + |
| 34 | +//question 19 |
| 35 | +//create a fucntion that will return in an array the first "nprimes" prime |
| 36 | +// numbres greater than a particular number startAt |
| 37 | +// means that i need to create a function that will take two input |
| 38 | +//and return the desired ouput |
| 39 | + |
| 40 | +// here in this code what i need to do is that i need to find the |
| 41 | +//prime number after a certain point that is it |
| 42 | + |
| 43 | +getprime(10,5) |
| 44 | +function getprime(nprimes, start){ |
| 45 | +count =0 |
| 46 | + while(count<nprimes){ |
| 47 | + if(prime_hai(start)){ |
| 48 | + console.log(start) |
| 49 | + count++ |
| 50 | + } |
| 51 | + start++ |
| 52 | + } |
| 53 | +} |
| 54 | +function prime_hai(start){ |
| 55 | + if(start<2){return false |
| 56 | + } |
| 57 | + for(let i=2;i<=Math.sqrt(start);i++){ |
| 58 | + if(start%i==0){ |
| 59 | + return false |
| 60 | + } |
| 61 | + }return true |
| 62 | +} |
| 63 | + |
| 64 | +//question 20 |
| 65 | +//rotate the an array to the left one(1) position |
| 66 | + |
| 67 | +// this kind of questions are very easy in the js as it can be easily be done with the help of the shift method in the js |
| 68 | +function shift_karna(arr){ |
| 69 | + let temp=arr.shift() |
| 70 | + arr.push(temp) |
| 71 | + return console.log(arr) |
| 72 | +} |
| 73 | +let arr=[1,2,3,4,5] |
| 74 | +shift_karna(arr) |
| 75 | + |
| 76 | + |
| 77 | +//question 21 |
| 78 | +// rotate an array to the right by 1 position |
| 79 | + |
| 80 | +function right_kro(arr1){ |
| 81 | + let temp=arr1.pop() |
| 82 | + arr1.unshift(temp) |
| 83 | + return console.log(arr1) |
| 84 | +} |
| 85 | +let arr1=[1,2,3,4] |
| 86 | +right_kro(arr1) |
| 87 | + |
0 commit comments