Using Java create a few functions to work with natural numbers. However, there a
ID: 644156 • Letter: U
Question
Using Java create a few functions to work with natural numbers. However, there are a few restrictions on what you can use in this program. For starters, you should be working entirely with arrays, so no lists, array lists, linked lists, vectors, sets, dictionaries or any other fancy collections. Second, you can't use any math functions that require importing a package. So Math stuff is okay (like Math.pow() and Math.sqrt(), but definitely nothing from java.math.BigInteger (like BigInteger.gcd()).
Create a class NumberTheory with the following static methods:
1. generateMersennes() takes an int and returns an array consisting of that many Mersenne numbers. A Mersenne number is of the form 2^n -1 for some n. So the first 5 Mersenne number are 2^1 -1=1, 2^2 -1=3, 2^3 -1 =7, 2^4 -1=15, and 2^5 -1=31. So generateMersennes(5) should return the array [1,3,7,15,31].
2. numFactors() takes an int and returns how many factors that number has (including 1 and the number itself). Remember that a factor of a number is any number that divides evenly into it. So numFactors(12) should return 6 because 12 has the factors 1,2,3,4,6, and 12.
3. factorsOf() takes an int and returns an array of all the factors of that number (including 1 and the number itself). So factorOf(12) should return the array [1,2,3,4,6,12].
4. isPrime() takes an int and returns true if the number is prime. Remember that a prime number has exactly two factors: 1 and the number itself (1 is not prime). So isPrime(12) should return false and isPrime(17) should return true.
5. numPrimes() takes an array of ints and returns how many prime numbers are in the array. So if arr1 is the array [1,2,3,4,5,6,7,8,9], then numPrimes(arr1) should return 4 because in that array, only 2,3,5,and 7 are prime.
6. filterPrimes() takes an array of ints and returns a version of that array with only the prime numbers in it. So if arr1 is the array [1,2,3,4,5,6,7,8,9], then filterPrimes(arr1) should return the array [2,3,5,7].
Finally, your NumberTheory class should have a main() method which uses generateMersennes() to create an array of the first 25 Mersenne numbers and then uses filterPrimes() to find out which of the first 25 Mersenne numbers are prime. It should first display all of the Mersenne numbers that were generated and then display all of those that are prime. You can format things however you want, but it should be clear what those numbers represent.
Explanation / Answer
package com.java2novice.algos;
public class MyPrimeNumCheck {
public boolean isPrimeNumber(int number){
for(int i=2; i<=number/2; i++){
if(number % i == 0){
return false;
}
}
return true;
}
public static void main(String a[]){
MyPrimeNumCheck mpc = new MyPrimeNumCheck();
System.out.println("Is 17 prime number? "+mpc.isPrimeNumber(17));
System.out.println("Is 19 prime number? "+mpc.isPrimeNumber(19));
System.out.println("Is 15 prime number? "+mpc.isPrimeNumber(15));
}
}
- See more at: http://java2novice.com/java-interview-programs/is-prime-number/#sthash.yinKkrEt.dpuf
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.