|
| 1 | +```Javascript |
| 2 | +#Javascript |
| 3 | + |
| 4 | +function findPrimes(numberToCheck) { // A machine that helps find prime numbers |
| 5 | + |
| 6 | + if (numberToCheck <=1){ |
| 7 | + return `${numberToCheck} is not a prime number.`; //any number that is less or equal to 1 it is NOT a prime number |
| 8 | + } |
| 9 | + let isPrime = true; //I am start with assuming the number is prime |
| 10 | + |
| 11 | + |
| 12 | + for (let factor = 2; factor <= Math.floor(numberToCheck / 2); factor++) { //this is another loop but it checks to see if the number is divisible by other numbers. |
| 13 | + if (numberToCheck % factor === 0) { // this is checking to see if the number can divide evenly and if so then it is not a prime number |
| 14 | + isPrime = false; // states that the number is not prime if it comes out as 0 (should have a remainder) |
| 15 | + break; //states that it can STOP the loop once it finds a 0, no need to keep going through |
| 16 | + } //this is closing the loop |
| 17 | + } |
| 18 | + |
| 19 | + if (isPrime) { //if said number is still true that means that we did not find any number that is divided evenly so it is prime |
| 20 | + return `${numberToCheck} is a prime number.`; //if the numbe is prime it will say^^ |
| 21 | + } else{ |
| 22 | + return `${numberToCheck} is not a prime number.`; // if it is NOT prime it will say so |
| 23 | + } |
| 24 | +} //closing the loop of if it is a prime number or not |
| 25 | + |
| 26 | +const input = process.argv[2]; // telling the computer to save the 3rd thing you type |
| 27 | + |
| 28 | +let number = parseInt(input); // if it types a word the computer does the math and makes it a number |
| 29 | + |
| 30 | + if (isNaN(number)) { // make sure you type a number |
| 31 | + console.log("Please enter a valid number."); // letting the user know you have to type a number |
| 32 | + } else { |
| 33 | + console.log(findPrimes(number)); //now the computer can check if it is prime or not |
| 34 | + } |
| 35 | + |
| 36 | +// credit from Coding with John youtube video https://www.youtube.com/watch?v=I9-3drnfyp0 and Chatgpt for a explanation of things I still might have been confused about |
| 37 | +``` |
| 38 | + |
| 39 | +```python |
| 40 | +# Python |
| 41 | +# this is a function that will help us find all the prime numbers |
| 42 | +def find_primes(number_to_check): |
| 43 | + if number_to_check <= 1: # this is an empty list for now until we run the test for all the prime numbers we find |
| 44 | + return f"{number_to_check} is not a prime number." |
| 45 | + |
| 46 | + is_prime = True # I am saying that it is a prime until I find out it is not |
| 47 | + |
| 48 | + # checks to see if the number can be divided by a smaller number evenly |
| 49 | + for factor in range(2, number_to_check // 2 + 1): |
| 50 | + if number_to_check % factor == 0: # trying to see if there is a remainder after the divison and if it is equal to zero |
| 51 | + is_prime = False # if it is equal to zero it is flase meaning it is not prime |
| 52 | + break # again it means STOP |
| 53 | + if is_prime: # after checking all |
| 54 | + return f"{number_to_check} is a prime number." |
| 55 | + else: |
| 56 | + return f"{number_to_check} is not a prime number." |
| 57 | + |
| 58 | +number = int(input("Enter a number to check to see if its prime: ")) |
| 59 | +print(find_primes(number)) |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +## Explanation |
| 64 | +My first thought was to use Javascript and html because those were the 2 languages that I was familiar with. I did some research and quickly came to the realization that html would not be the most effective. That's when I found out that I should use Python and Javascript. |
| 65 | +Python is known for how easy it is to read and how simple it is. But is super space indentation sensitive. Whereas Javascript is a little more complex because it uses different characters, which makes it a little harder to understand. |
| 66 | + |
| 67 | +Similarities: Both Javascript and Python are finding prime numbers between 2 and 100 even though in pyton it says 101. That is becuase we did not plug in 101 we stopped right before it. |
| 68 | + |
| 69 | +Diffreneces: A diffrence that Javascript uses let for declaring variables while python uses simplier words becuase you do not need a keyword |
| 70 | + |
0 commit comments