Goldbach's Conjecture

Goldbach's conjecture is a rule in math that states the following: every even number greater than 2 can be expressed as the sum of two prime numbers.

Write a program that finds every possible pair of prime numbers, whose sum equals the given number or a set of numbers within a range.

For example:
Input: 16
Output:
3 + 13
5 + 11

Input: 32
Output:
3 + 29
13 + 19

Input: 4, 8
Output:
4: 2 + 2
6: 3 + 3
8: 3 + 5


Video: https://youtu.be/5ivXh78VRD0

Program:

goldbachConjecture.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Goldbach's Conjecture</title>
    <script src="goldbachConjecture.js"></script>
</head>
<body>
    <input type="text" name="firstNumber" id="firstNumber" placeholder="Enter first number">
    <input type="text" name="secondNumber" id="secondNumber" placeholder="Enter second number">
    <button onclick="checkForConjecture()" >Check</button>
    <p id="results"></p>
</body>
</html>


goldbachConjecture.js


// This will be the global function that will be called from html on button click
function checkForConjecture() {
    // getValues of the input
    const firstNumber = +getElement('firstNumber').value;
    const secondNumber = +getElement('secondNumber').value;

    // Alert user if no values are entered
    if (!firstNumber && !secondNumber) {
        alert('Please enter any value');
        return;
    }

    // create an array which will contain all the prime numbers
    let primesArray = [];

    // For range we will have to check if both numbers are entered
    // This condition must be before the previous one as if any value is entered then both will be called
    if (firstNumber && secondNumber) {
        for (let index = 2; index <= secondNumber; index++) {
            if (isPrime(index)) {
                primesArray.push(index);
            }
        }
        checkConjecture(primesArray, firstNumber, secondNumber);
        return;
    }

    // First we will check either first or second number is entered then we have to find conjecture
    if (firstNumber || secondNumber) {
        let number = firstNumber || secondNumber;
        for (let index = 2; index <= number; index++) {
            // check if the number is prime
            if (isPrime(index)) {
                primesArray.push(index);
            }
        }
        // now that we are getting all the prime number we will find the values that matches the criteria
        // null is passed for the range
        checkConjecture(primesArray, number, null);
    }
}

// create a function to get the html element value from dom
function getElement(id) {
    return document.getElementById(id);
}

// function to check if the number is prime or not
function isPrime(number) {
    for(let index = 2; index< number; index++) {
        if (number % index == 0) {
            return false;
        }
    }
    return true;
}

// function to check for conjecture
function checkConjecture(primesArray, firstNumber, secondNumber) {
    // if the second / max range is provided then we will call another function for range
    if (secondNumber) {
        checkConjectureWithinRange(primesArray, firstNumber, secondNumber);
        return;
    }
    getConjectureValues(primesArray, firstNumber);
}

// function to print the value that matches for the number
function getConjectureValues(primesArray, firstNumber) {
    // now we will loop through the primesArray and check which number adds up for firstnumber
    for (let index = 0; index < primesArray.length; index++) {
        const element = primesArray[index];
        for (let index2 = index; index2 < primesArray.length; index2++) {
            const element2 = primesArray[index2];
            if (element + element2 == firstNumber) {
                // print the result into dom
                printResults(element, element2, firstNumber);
            }
        }
        
    }
}

// now we will match the criteria for range
function checkConjectureWithinRange(primesArray, firstNumber, secondNumber) {
    for(let index = firstNumber; index <= secondNumber; index++) {
        // check only for even numbers
        if (index % 2 == 0) {
            getConjectureValues(primesArray, index);
        }
    }
}

// Function to print the result into the html document
function printResults(element, element2, data) {
    getElement('results').innerHTML += `${element} + ${element2} = ${data} <br/>`;
}

Comments

Popular Posts