A Java source code that All functions called methods in Java

A Java source code that:

All functions (called methods in Java) shall be included in a single file

The code shall output the number of prime numbers in the following ranges

[1, 100]

[1, 1000]

[1, 10000]

[1, 100000]

[1, 1000000]

[1, 10000000]

[7870000000, 7879999999]

[9390000000, 9399999999]

The code shall also output the last five prime numbers in each range

The code shall also output the first five prime numbers in ranges (vii) and (viii).

The code shall also output the amount of time in seconds to find all prime numbers in each range

Solution

Here is the code for you:

class PrimeNumberExperiment
{
public static boolean isPrime(long number)
{
for(long i = 2; i <= Math.sqrt(number); i++)   //Run the loop from 2 to sqrt(number).
if(number % i == 0)                       //If the number is divisible by any value of i.
return false;                               //The number is not prime.
return true;                                 //If all the values exhaust, the number is prime.
}
public static int countPrimesBetween(long start, long end)
{
int count = 0;
for(long i = start; i <= end; i++)   //For each number in the specified range.
if(isPrime(i))                       //If the number is prime.
count++;                           //Increment the counter.
return count;                         //Return the count.
}
public static void printLastFivePrimes(long start, long end)
{
int count = 0;
for(long i = end; i >= start && count != 5; i--)
if(isPrime(i))
{
System.out.print(i + \" \");
count++;
}
System.out.println();   
}
public static void printFirstFivePrimes(long start, long end)
{
int count = 0;
for(long i = start; i <= end && count != 5; i++)
if(isPrime(i))
{
System.out.print(i + \" \");
count++;
}
System.out.println();
}
public static void main(String[] args)
{
long start, end;
System.out.println(\"The number of primes in the range [1, 100] is: \" + countPrimesBetween(1, 100));
System.out.println(\"The number of primes in the range [1, 1000] is: \" + countPrimesBetween(1, 1000));
System.out.println(\"The number of primes in the range [1, 10000] is: \" + countPrimesBetween(1, 10000));
System.out.println(\"The number of primes in the range [1, 100000] is: \" + countPrimesBetween(1, 100000));
System.out.println(\"The number of primes in the range [1, 1000000] is: \" + countPrimesBetween(1, 1000000));
System.out.println(\"The number of primes in the range [1, 10000000] is: \" + countPrimesBetween(1, 10000000));
System.out.println(\"The number of primes in the range [7870000000, 7879999999] is: \" + countPrimesBetween(7870000000, 7879999999));
System.out.println(\"The number of primes in the range [9390000000, 9399999999] is: \" + countPrimesBetween(9390000000, 9399999999));
System.out.println(\"The first 5 primes in the range [1, 100] are: \" + printFirstFivePrimes(1, 100));
System.out.println(\"The last 5 primes in the range [9390000000, 9399999999] are: \" + printLastFivePrimes(9390000000, 9399999999));
  
}
}

A Java source code that: All functions (called methods in Java) shall be included in a single file The code shall output the number of prime numbers in the foll
A Java source code that: All functions (called methods in Java) shall be included in a single file The code shall output the number of prime numbers in the foll

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site