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 $1 if you
register a new account!

Assisting Tutor

Related Homework Solutions

Tic-Tac-Toe Program in Java
Homework Solution
$40.00
Programming
Java
Coding
Tic-Tac-Toe
Game Development
Array
Private Data Members
Board
Players
Public Data Members
UML Diagram
Statements
Variables
Wins
Loses
Ties
Java Programming: Arrays Of Marks & Circles
Homework Solution
$50.00
Java
Programming
Codes
Computer Science
Algorithms
Arrays
Marks
Circles
Geometry
Mathematics
Radius
Random Numbers
Boundaries
Statements
Loops
Variables
Points
Coordinates
Perimeters
Area
Parking Problem in Java
Homework Solution
$30.00
Java
Programming
Codes
Classes
Computer Science
Parking
OOP
Car Model
License Number
Owner Name
Strings
Integers
Statements
Input
Output
Constructor
Public Methods
Functions
Explanation For Java Classes
Homework Solution
$30.00
Java
Programming
Computer Science
Codes
Classes
Inheritance
Comparator
Binary Matrix
Labels
Edges
Tree
Strings
LinkedList
Integers
Nodes
Temporary Storage
Statements
Variables
Species
HashSet
Iterator
Java Programming Assignment
Homework Solution
$30.00
Java Programming
Fibonacci NumbersIntegers
Computer Science
Pseudocode
Program
Code
Get help from a qualified tutor
Live Chats