Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lesson_04/PrimeCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class PrimeCheck {
public static void main(String[] args) {
int number = 29; // Change this number to test other values

if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is NOT a prime number.");
}
}

// Method to check if a number is prime
public static boolean isPrime(int num) {
if (num <= 1) {
return false; // 0 and 1 are not prime
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false; // Found a divisor, so not prime
}
}
return true; // No divisors found, so prime
}
}
Binary file added lesson_04/TrishtanH/PrimeCheck.class
Binary file not shown.
24 changes: 24 additions & 0 deletions lesson_04/TrishtanH/PrimeCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class PrimeCheck {
public static void main(String[] args) {
int number = 29; // Change this number to test other values

if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is NOT a prime number.");
}
}

// Method to check if a number is prime
public static boolean isPrime(int num) {
if (num <= 1) {
return false; // 0 and 1 are not prime
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false; // Found a divisor, so not prime
}
}
return true; // No divisors found, so prime
}
}
Binary file added lesson_04/TrishtanH/PrimeCheckTest.class
Binary file not shown.
84 changes: 84 additions & 0 deletions lesson_04/TrishtanH/PrimeCheckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
public class PrimeCheckTest {
private static int testsPassed = 0;
private static int totalTests = 0;

public static void main(String[] args) {
System.out.println("Running Java Prime Number Tests...\n");

// Test edge cases
testPrimeCheck("0 should not be prime", 0, false);
testPrimeCheck("1 should not be prime", 1, false);
testPrimeCheck("-5 should not be prime", -5, false);

// Test small prime numbers
testPrimeCheck("2 should be prime", 2, true);
testPrimeCheck("3 should be prime", 3, true);
testPrimeCheck("5 should be prime", 5, true);
testPrimeCheck("7 should be prime", 7, true);

// Test small composite numbers
testPrimeCheck("4 should not be prime", 4, false);
testPrimeCheck("6 should not be prime", 6, false);
testPrimeCheck("8 should not be prime", 8, false);
testPrimeCheck("9 should not be prime", 9, false);
testPrimeCheck("10 should not be prime", 10, false);

// Test larger prime numbers
testPrimeCheck("11 should be prime", 11, true);
testPrimeCheck("13 should be prime", 13, true);
testPrimeCheck("17 should be prime", 17, true);
testPrimeCheck("19 should be prime", 19, true);
testPrimeCheck("23 should be prime", 23, true);
testPrimeCheck("29 should be prime", 29, true);

// Test larger composite numbers
testPrimeCheck("12 should not be prime", 12, false);
testPrimeCheck("15 should not be prime", 15, false);
testPrimeCheck("21 should not be prime", 21, false);
testPrimeCheck("25 should not be prime", 25, false);
testPrimeCheck("30 should not be prime", 30, false);

// Test perfect squares
testPrimeCheck("16 (4²) should not be prime", 16, false);
testPrimeCheck("25 (5²) should not be prime", 25, false);
testPrimeCheck("49 (7²) should not be prime", 49, false);

// Print summary
System.out.println("\nTest Results:");
System.out.println("Tests passed: " + testsPassed + "/" + totalTests);
System.out.println("Success rate: " + (testsPassed * 100 / totalTests) + "%");

if (testsPassed == totalTests) {
System.out.println("🎉 All tests passed!");
} else {
System.out.println("❌ Some tests failed.");
}
}

private static void testPrimeCheck(String testName, int number, boolean expected) {
totalTests++;
boolean actual = PrimeCheck.isPrime(number);

if (actual == expected) {
System.out.println("✅ PASS: " + testName);
testsPassed++;
} else {
System.out.println("❌ FAIL: " + testName +
" - Expected: " + expected + ", Got: " + actual);
}
}

// Copy of the isPrime method from PrimeCheck class for testing
// In a real scenario, we'd import this from the PrimeCheck class
public static boolean isPrime(int num) {
if (num <= 1) {
return false; // 0 and 1 are not prime
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false; // Found a divisor, so not prime
}
}
return true; // No divisors found, so prime
}
}
88 changes: 88 additions & 0 deletions lesson_04/TrishtanH/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
## Java implementation

```java
public class PrimeCheck {
public static void main(String[] args) {
int number = 29; // Change this number to test other values

if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is NOT a prime number.");
}
}

// Method to check if a number is prime
public static boolean isPrime(int num) {
if (num <= 1) {
return false; // 0 and 1 are not prime
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false; // Found a divisor, so not prime
}
}
return true; // No divisors found, so prime
}
}
```

## JavaScript implementation

```javascript
function isPrime(number) {
// Numbers less than or equal to 1 are not prime
if (number <= 1) {
return false;
}

// Check for divisors up to square root of number
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false; // Found a divisor, so not prime
}
}

return true; // No divisors found, so prime
}

// Test prime numbers
console.log("Is 7 prime?", isPrime(7)); // Output: true
console.log("Is 4 prime?", isPrime(4)); // Output: false
console.log("Is 13 prime?", isPrime(13)); // Output: true
console.log("Is 29 prime?", isPrime(29)); // Output: true
```

## Explanation

Both implementations use the same mathematical approach to determine if a number is prime: they check if the number has any divisors between 2 and the square root of the number. The Java implementation is structured as a class with a main method that demonstrates the prime checking functionality, while the JavaScript implementation uses a standalone function with multiple test cases.

### Similarities

1. **Core Algorithm**: Both implementations use the same efficient prime-checking algorithm that only tests divisors up to the square root of the input number.
2. **Edge Case Handling**: Both correctly handle the edge case where numbers less than or equal to 1 are not considered prime.
3. **Logic Flow**: The control flow is identical - check edge cases first, then loop through potential divisors, returning false if any divisor is found.
4. **Performance**: Both use the optimized approach of only checking up to Math.sqrt(number), making them equally efficient.

### Differences

1. **Language Structure**:
- **Java** requires a class definition and uses a static main method as the entry point for execution.
- **JavaScript** runs functions and code directly without requiring a class structure.

2. **Type System**:
- **Java** uses static typing with explicit type declarations (`int num`, `boolean isPrime`).
- **JavaScript** uses dynamic typing where variables can hold any type without declaration.

3. **Compilation vs Interpretation**:
- **Java** is a compiled language that must be compiled to bytecode before execution.
- **JavaScript** is interpreted at runtime by the JavaScript engine.

4. **Testing Approach**:
- **Java** implementation tests a single number (29) and prints a descriptive message.
- **JavaScript** implementation tests multiple numbers (7, 4, 13, 29) with concise output showing the function's return values.

5. **Syntax Differences**:
- **Java** uses `System.out.println()` for output while **JavaScript** uses `console.log()`.
- **Java** uses `Math.sqrt()` within a class context while **JavaScript** accesses it directly.
- Variable declarations differ: `int i` in Java vs `let i` in JavaScript.
21 changes: 21 additions & 0 deletions lesson_04/TrishtanH/primechecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function isPrime(number) {
// Numbers less than or equal to 1 are not prime
if (number <= 1) {
return false;
}

// Check for divisors up to square root of number
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false; // Found a divisor, so not prime
}
}

return true; // No divisors found, so prime
}

// Test prime numbers
console.log("Is 7 prime?", isPrime(7)); // Output: true
console.log("Is 4 prime?", isPrime(4)); // Output: false
console.log("Is 13 prime?", isPrime(13)); // Output: true
console.log("Is 29 prime?", isPrime(29)); // Output: true
83 changes: 83 additions & 0 deletions lesson_04/TrishtanH/primechecker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Import the isPrime function (assuming it's exported)
// For this simple test, we'll include the function directly

function isPrime(number) {
// Numbers less than or equal to 1 are not prime
if (number <= 1) {
return false;
}

// Check for divisors up to square root of number
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false; // Found a divisor, so not prime
}
}

return true; // No divisors found, so prime
}

// Simple test framework
function test(description, testFunction) {
try {
testFunction();
console.log(`✅ PASS: ${description}`);
} catch (error) {
console.log(`❌ FAIL: ${description} - ${error.message}`);
}
}

function assertEqual(actual, expected, message) {
if (actual !== expected) {
throw new Error(message || `Expected ${expected}, but got ${actual}`);
}
}

// Unit Tests
console.log('Running JavaScript Prime Number Tests...\n');

test('should return false for numbers less than 2', () => {
assertEqual(isPrime(0), false, '0 should not be prime');
assertEqual(isPrime(1), false, '1 should not be prime');
assertEqual(isPrime(-5), false, 'negative numbers should not be prime');
});

test('should return true for small prime numbers', () => {
assertEqual(isPrime(2), true, '2 should be prime');
assertEqual(isPrime(3), true, '3 should be prime');
assertEqual(isPrime(5), true, '5 should be prime');
assertEqual(isPrime(7), true, '7 should be prime');
});

test('should return false for small composite numbers', () => {
assertEqual(isPrime(4), false, '4 should not be prime');
assertEqual(isPrime(6), false, '6 should not be prime');
assertEqual(isPrime(8), false, '8 should not be prime');
assertEqual(isPrime(9), false, '9 should not be prime');
assertEqual(isPrime(10), false, '10 should not be prime');
});

test('should return true for larger prime numbers', () => {
assertEqual(isPrime(11), true, '11 should be prime');
assertEqual(isPrime(13), true, '13 should be prime');
assertEqual(isPrime(17), true, '17 should be prime');
assertEqual(isPrime(19), true, '19 should be prime');
assertEqual(isPrime(23), true, '23 should be prime');
assertEqual(isPrime(29), true, '29 should be prime');
});

test('should return false for larger composite numbers', () => {
assertEqual(isPrime(12), false, '12 should not be prime');
assertEqual(isPrime(15), false, '15 should not be prime');
assertEqual(isPrime(21), false, '21 should not be prime');
assertEqual(isPrime(25), false, '25 should not be prime');
assertEqual(isPrime(30), false, '30 should not be prime');
});

test('should handle perfect squares correctly', () => {
assertEqual(isPrime(16), false, '16 (4²) should not be prime');
assertEqual(isPrime(25), false, '25 (5²) should not be prime');
assertEqual(isPrime(49), false, '49 (7²) should not be prime');
});

console.log('\nJavaScript tests completed!');
Loading