Skip to content

Commit 76abc20

Browse files
committed
Prime numbers
1 parent 33090f9 commit 76abc20

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

lesson_04/dasiaenglish/lesson_04.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## Javascript
2+
```javascript
3+
function findPrimes() { // A machine that helps find prime numbers
4+
let primeNumbers = []; // Use 'let' and an empty array for prime numbers
5+
6+
for (let numberToCheck = 2; numberToCheck <= 100; numberToCheck++) { // Here is a loop that starts at 2 and keeps going until 100
7+
let isPrime = true; //I am starting this out with assuming the number is true
8+
9+
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.
10+
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
11+
isPrime = false; // states that the number is not prime if it comes out as 0 (should have a remainder)
12+
break; //states that it can STOP the loop once it finds a 0, no need to keep going through
13+
} //this is closing the loop
14+
}
15+
16+
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
17+
primeNumbers.push(numberToCheck); // Push primes into the array box above []
18+
}
19+
} //closing the loop from 2 to 100
20+
21+
22+
//Output the prime numbers
23+
console.log("Prime numbers from 1 to 100 are:"); // telling the comuter to print it out what is in " "
24+
console.log(primeNumbers.join(", ")); // show the prime numbers that we found
25+
} //will fill in all the numbers that are prime
26+
27+
// Call the function to find and print primes
28+
findPrimes();
29+
30+
// credit from Coding with John youtube video https://www.youtube.com/watch?v=I9-3drnfyp0
31+
```
32+
33+
34+
## Python
35+
```python
36+
def find_primes(): # this is a function that will help us find all the prime numbers
37+
prime_numbers = [] # this is an empty list for now until we run the test for all the prime numbers we find
38+
39+
for number_to_check in range(2, 101): # checking numbers from 2 to 100
40+
is_prime = True # I am saying that it is a prime until I find out it is not
41+
42+
for factor in range(2, number_to_check // 2 + 1): # checks to see if the number can be divided by a smaller number evenly
43+
if number_to_check % factor == 0: # trying to see if there is a remainder after the divison and if it is equal to zero
44+
is_prime = False # if it is equal to zero it is flase meaning it is not prime
45+
break # again it means STOP
46+
if is_prime: # after checking all
47+
prime_numbers.append(number_to_check) # Add prime numbers
48+
49+
print("Prime numbers from 1 to 100 are:")
50+
# type out what is in the " "
51+
print(", ".join(map(str, prime_numbers))) # put all the prime numbers split by a commma
52+
find_primes() #tells the computer to go ahead and run the function
53+
```
54+
## Explanation
55+
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.
56+
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.
57+
58+
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.
59+
60+
Diffreneces: A diffrence that Javascript uses let for declaring variables while python uses simplier words becuase you do not need a keyword
61+

0 commit comments

Comments
 (0)