Subject Computer Science Java Programming

Question

Write a program in java to test GoldBach's conjecture for numbers between 900000 and 900100 and optimize running time.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

    static boolean IsPrime(int n) // n is between 6 and 40000000
    {
       int i = 5;
       boolean prime = true;
       if ((n > 3) && (((n % 2) == 0) || ((n % 3) == 0))) // check if it is dividable with 2 or 3, if yes it is not a prime
       {
            prime = false;
       }
       while ((prime) && ((i * i) <= n)) // Check candidates to squareroot of n, if there is a divisor at least one is less than the squareroot
       {
            /*
            The candidates for prime numbers are 6k +- 1, k is a whole positive number
            6k, 6k + 2 and 6k + 4 are dividable by 2
            6k + 3 is dividable by 3
            So only 6k +1 and 6k +5 (same as 6k -1 of next k) can be primes
            i starts from 5, which is for k = 1 6k -1
            Then i and i + 2, which is 6k + 1 is checked if they divide n with mod 0, if yes, n is not prime
            i is increased by 6, same as k is incremented by 1
            */
            prime = (((n % i) != 0) && ((n % (i + 2)) != 0));
            i += 6;
       }
       return prime;
    }...

This is only a preview of the solution. Please use the purchase button to see the entire solution

$13.00

or free if you
register a new account!

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats