Skip to content

Commit 8d30a82

Browse files
authored
feat: tommy tran lesson_4 function added in java and js (#141)
* feat:lesson_4 function added in java and js * fix: added additional information to readme.md
1 parent 556b095 commit 8d30a82

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

lesson_04/tommytran/lesson_4.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class lesson_4 {
2+
3+
// Function to check if a number is prime
4+
public static boolean isPrime(int n) {
5+
if (n < 2) {
6+
return false; // Numbers less than 2 are not prime
7+
}
8+
if (n == 2) {
9+
return true; // 2 is the only even prime number
10+
}
11+
if (n % 2 == 0) {
12+
return false; // Numbers divisible by 2 are not prime
13+
}
14+
// Loop that checks every odd number up to the square root of n
15+
for (int i = 3; i <= Math.sqrt(n); i += 2) {
16+
if (n % i == 0) {
17+
return false; // n is divisible by i, so it's not prime
18+
}
19+
}
20+
return true; // n is prime
21+
}
22+
23+
public static void main(String[] args) {
24+
// Declare integers in main method
25+
int a = 1;
26+
int b = 9;
27+
int c = 17;
28+
29+
// Return string of whether or not int is a prime number
30+
System.out.println("Is " + a + " a prime number? " + isPrime(a));
31+
System.out.println("Is " + b + " a prime number? " + isPrime(b));
32+
System.out.println("Is " + c + " a prime number? " + isPrime(c));
33+
}
34+
}
35+
// code sourced from chat-gpt after converting my JS file.

lesson_04/tommytran/lesson_4.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
//declare integers in function: sample numbers
3+
let a = 1;
4+
let b = 9;
5+
let c = 17;
6+
{
7+
function Prime(n) {
8+
if (n < 2) {
9+
return false; // Numbers less than 2 are not prime
10+
}
11+
if (n === 2) {
12+
return true; // 2 is the only even prime number
13+
}
14+
if (n % 2 === 0) {
15+
return false; // numbers divisible by 2 are not prime
16+
}
17+
// loop that checks every odd number up until square root of n
18+
for (let i = 3; i <= Math.sqrt(n);i +=2) {
19+
if (n % i === 0) {
20+
return false; // n is divisible by i, so it's not prime
21+
}
22+
}
23+
return true; // n is prime
24+
}
25+
//return string of whether or not int is a prime number
26+
console.log(`Is ${a} a prime number? ${Prime(a)}`);
27+
console.log(`Is ${b} a prime number? ${Prime(b)}`);
28+
console.log(`Is ${c} a prime number? ${Prime(c)}`);
29+
}

lesson_04/tommytran/readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Explanation
2+
3+
4+
First I declared 3 variables to use to test my function a,b, and c. I then assigned 3 integers to these variables to test this function. I created a function by the name of prime that contained an if else statement and a for loop. I then created several rules for the the if statement. The first rule determined whether the number was less than 2 then it was considered not a prime number. The second rule was that the number 2 would return true as it is the only even prime number. the third rule was that any number that was divided by 2 would not leave a remainder a 0. If none of those conditions were satisfied then a for loop would activate starting from the number 3. The loop would check all numbers starting at number #3 and must be less than or equal to the square root of (n). The loop would then increase integer by 2 to check every odd number between 3 and the square root. If any number that was determined (n) to be divisible by (i) then it was false and not a prime number. If there was no odd divisble numbers then the statement would return true. Afterwards I would test the function by inputting a console.log command in the terminal to print a string that determined whether or not the number in each variable was a prime number or not.
5+
6+
7+
### Similiarities and Differences
8+
9+
10+
1. **Syntax**:
11+
- to print messages in the console in javascript you would do console.log
12+
- to print messages in the console in Java you would use the System.out.println command
13+
- although they are different they ultimately perform the same action and the function's are built in a very similar order/pattern.
14+
15+
2. ** Variables
16+
- In javascript you can declare a variablle using let and achieve a more broader scope of defined objects whereas in Java you have to be more specific with the data type as an int,double,string,etc.
17+
18+
3. ** Testing
19+
- to run the java you would name the file with a .java at the end and type "java [filename.java]" to run the program
20+
- to run the javascrip you would name the file with a .js at the end and type node [filename.js] to run the program

0 commit comments

Comments
 (0)