Skip to content

Commit 27f635a

Browse files
feat: adds Dylan's code samples (#154)
* Feat: Added Java and JavaScript Functions to find a prime number with explanations on how it works and hows its different. * Chore: Fixing Grammar issues. * Getting rid of my Java and Java script test files * Chore: Grammar Fixes * chore: revert Anthony's README Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent 872b4b0 commit 27f635a

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

lesson_04/dylanlafferty/ReadME.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## Java Implementation
2+
3+
```class PrimeChecker {
4+
public static boolean isPrime(int number) {
5+
if (number <= 1) {
6+
return false;
7+
}
8+
9+
// Check if number is divisible by any number from 2 to sqrt(number)
10+
for (int i = 2; i <= Math.sqrt(number); i++) {
11+
if (number % i == 0) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
18+
public static void main(String[] args) {
19+
int num1 = 11; //This number may be changed to change the input
20+
int num2 = 15; //This one too
21+
System.out.println(num1 + " is prime: " + isPrime(num1));
22+
System.out.println(num2 + " is prime: " + isPrime(num2));
23+
}
24+
}
25+
```
26+
27+
## JavaScript implementation
28+
29+
```
30+
// let number = prompt("Enter a number to check if it is a Prime number");
31+
32+
function checkPrime(number) {
33+
if (number === null || number === undefined) {
34+
console.log("You have not entered a number");
35+
return;
36+
}
37+
38+
if (number <= 1) {
39+
console.log("This is not a prime number");
40+
return false; // Numbers less than or equal to 1 are not prime
41+
}
42+
43+
for (let i = 2; i < Math.sqrt(number); i++) {
44+
if (number % i === 0) {
45+
console.log("This is not a prime number");
46+
return false; // If divisible by any number other than 1 and itself
47+
}
48+
}
49+
50+
console.log("This is a prime number");
51+
return true; // If no divisors were found, the number is prime
52+
}
53+
54+
console.log(checkPrime(45)); //
55+
console.log(checkPrime(23)); //
56+
console.log(checkPrime(13)); //
57+
```
58+
59+
## Explaination
60+
61+
The Java
62+
The Java implementation uses a class named `PrimeChecker`. `PrimeChecker` will get an integer named `number` and run it through 2 tests. The first test will be to see if `number` is less than or greater than 1 which then will return `false`. It will then see if the number is able to be divided by anything other than itself and if not then it will return `True`.
63+
64+
The JavaScript implementation uses a function named `checkPrime`. `CheckPrime` first checks if a `number` has been entered. If a `number` has been entered and if it is less than or equal to 1 it will return `True`. If this passes the for loop will run and see if that `number` can be divided into itself and if it can then return `False`.
65+
66+
## Similarites
67+
1. **Syntax**
68+
- Both languages write If statements in the same way using `if(instance) { code } `.
69+
- The For Loop are both written using `for` and adding the statements inside of `(statement1)`.
70+
- Math works the same in both languages using the `%` for division.
71+
2. **Input Fields**
72+
- Both Languages can have a predefined input inside of the code.
73+
- Both Languages can return either `true` or `false` to the console.
74+
75+
## Differences
76+
1. **Syntax**
77+
- In `Java` there are two Static functions but one it required to be defined as main. However in `JavaScript` you do not need to define a main function.
78+
- In `Java` in order to print something you have to write a `system.out.print` compared to `JavaScript` It uses the `console.log`.
79+
2. ## Declarations
80+
- With `Java` You have to let the computer know what type of input you are giving it. So if you would want to give a variable a number you need to declare that as an integer or `int`. While you can just write a number out in `javaScript`.
81+

0 commit comments

Comments
 (0)