You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lesson_04/dasiaenglish/lesson_04.md
+51-36Lines changed: 51 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,20 @@
1
-
## Javascript
2
-
```javascript
3
-
functionfindPrimes() { // A machine that helps find prime numbers
4
-
let primeNumbers = []; // Use 'let' and an empty array for prime numbers
1
+
```Javascript
2
+
#Javascript
3
+
4
+
constreadline=require(`readline`);
5
+
6
+
constrl=readline.createInterface({
7
+
input:process.stdin,
8
+
output:process.stdout
9
+
});
10
+
11
+
functionfindPrimes(numberToCheck) { // A machine that helps find prime numbers
12
+
13
+
if (numberToCheck <=1){
14
+
return`${numberToCheck} is not a prime number.`; //any number that is less or equal to 1 it is NOT a prime number
15
+
}
16
+
let isPrime =true; //I am start with assuming the number is prime
5
17
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
18
9
19
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
20
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,43 +24,48 @@ function findPrimes() { // A machine that helps find prime numbers
14
24
}
15
25
16
26
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
27
+
return`${numberToCheck} is a prime number.`; //if the numbe is prime it will say^^
28
+
} else{
29
+
return`${numberToCheck} is not a prime number.`; // if it is NOT prime it will say so
30
+
}
31
+
} //closing the loop of if it is a prime number or not
20
32
33
+
rl.question(`Enter a number to check if it\'s prime:`, (input)=>{
34
+
let number =parseInt(input);
21
35
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
36
+
if (isNaN(number)) {
37
+
console.log("Please enter a valid number.");
38
+
} else {
39
+
console.log(findPrimes(number));
40
+
}
41
+
});
42
+
// 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
43
+
```
26
44
27
-
// Call the function to find and print primes
28
-
findPrimes();
45
+
```python
46
+
# Python
47
+
# this is a function that will help us find all the prime numbers
48
+
deffind_primes(number_to_check):
49
+
if number_to_check <=1: # this is an empty list for now until we run the test for all the prime numbers we find
50
+
returnf"{number_to_check} is not a prime number."
29
51
30
-
// credit from Coding with John youtube video https://www.youtube.com/watch?v=I9-3drnfyp0
31
-
```
52
+
is_prime =True# I am saying that it is a prime until I find out it is not
32
53
54
+
# checks to see if the number can be divided by a smaller number evenly
55
+
for factor inrange(2, number_to_check //2+1):
56
+
if number_to_check % factor ==0: # trying to see if there is a remainder after the divison and if it is equal to zero
57
+
is_prime =False# if it is equal to zero it is flase meaning it is not prime
58
+
break# again it means STOP
59
+
if is_prime: # after checking all
60
+
returnf"{number_to_check} is a prime number."
61
+
else:
62
+
returnf"{number_to_check} is not a prime number."
63
+
64
+
number =int(input("Enter a number to check to see if its prime: "))
65
+
print(find_primes(number))
33
66
34
-
## Python
35
-
```python
36
-
deffind_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 inrange(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 inrange(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
67
```
68
+
54
69
## Explanation
55
70
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
71
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.
0 commit comments