Created
October 21, 2015 21:23
-
-
Save connlark/0d622c17433f0fe1bcb2 to your computer and use it in GitHub Desktop.
Recall that a prime number is an integer > 1 whose only factors are 1 and itself.
Create a class called Primes which will contain a class method called isPrime(int p) which determines if an integer p is a prime number. Assume p > 1.
Write this method without using any methods from the Math class.
All methods should be written as class methods us…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Connor on 10/19/15. | |
*/ | |
public class Primes { | |
//class method called isPrime(int p) which determines if an integer p is a prime number. Assume p > 1. | |
public static boolean isPrime(int p) { | |
if (p <= 1) return false; | |
if (p == 2) return true; | |
//check if n is a multiple of 2 | |
if (p%2==0) return false; | |
//if not, check the odds | |
for(int i = 3; i * i <= p; i += 2) { | |
if( p % i == 0) | |
return false; | |
} | |
return true; | |
} | |
//method that prints out the first 100 prime numbers. | |
public static void printPrimes(){ | |
int numbersPrinted = 0; | |
int i = 0; | |
while (numbersPrinted != 100){ | |
if (isPrime(i)){ | |
System.out.println(i); | |
numbersPrinted++; | |
} | |
i++; | |
} | |
} | |
//method override for printPrimes() method that prints out the first n prime numbers. | |
public static void printPrimes(int n){ | |
int numbersPrinted = 0; | |
int i = 0; | |
while (numbersPrinted < n){ | |
if (isPrime(i)){ | |
System.out.println(i); | |
numbersPrinted++; | |
} | |
i++; | |
} | |
} | |
//method that returns how many prime numbers are less than or equal to the integer max. Note: max could be negative. | |
public static int countPrime(int max){ | |
int count = 0; | |
if (max < 1) return 0; | |
for (int i = 0; i <= max; i++) { | |
if (isPrime(i)){ | |
count++; | |
} | |
} | |
return count; | |
} | |
//returns the sum of the first n prime numbers. | |
public static int sumPrime(int n) { | |
if (n < 0) return 0; | |
int sum = 0; | |
int primeCount = 0; | |
int i = 0; | |
while (primeCount < n){ | |
if (isPrime(i)){ | |
primeCount++; | |
sum += i; | |
} | |
i++; | |
} | |
return sum; | |
} | |
//returns the average of the first n prime numbers | |
public static double avePrime(int n){ | |
return (double) sumPrime(n) / n; | |
} | |
//returns the maximum “distance” between each pair of consecutive prime numbers <= n. | |
public static int primeSpace(int n){ | |
int lastPrime = 0; | |
int maxDistance = 0; | |
int newDistance; | |
for (int i = 0; i <= n; i++) { | |
if (isPrime(i)){ | |
newDistance = (i - lastPrime); | |
if (maxDistance < newDistance){ | |
maxDistance = newDistance; | |
} | |
lastPrime = i; | |
} | |
} | |
return maxDistance; | |
} | |
public static void main(String... args){ | |
System.out.println(sumPrime(5)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment