Showing NINE as a prime number while executing this code.Help me - java

//Getting Prime numbers from 2 to given range.
//When Inner for loop is running for number 2, it is printing 9 as a prime number.
import java.util.*;
import java.io.*;
class A
{
public static void main(String args[])
{
System.out.println("Enter the number till which the prime number is to be printed:");
Scanner sc = new Scanner(System.in);
int limit = sc.nextInt();
System.out.println("Printing the prime numbers from 1 to "+limit);
for(int num =2; num<=limit; num++)
{
for(int d=2; d<=num; d++)
{
if(num%d == 0)
{
break;
}
if()
{
System.out.println(num);
break;
}
}
}
}
}

Above the second loop keep a flag as true which means initially the number is a prime number.
boolean flag = true;
Second loop condition shall be d <= num/2
Inside the second loop to break if its not prime, or divisible by a number.
if(num%d == 0)
{
flag = false;
break;
}
Outside the second loop, print it if the flag is still true:
if(flag){
System.out.println(num);
}
Remember: For large scale prime number checking or generation, the algorithm of Sieve is always best.

I have a bad news. This program finds not the prime number but the odd numbers: 3, 5, 7, 9 etc.

You are printing the values if it is not divisible.
all the non prime numbers from 2 to 8 are divisible by 2 and hence it is showing only prime numbers.
in case when number is 9, it is printing 9 because 9%2 is not 0.

Related

Need help explaining how this prime number program works

I have this code here that asks a user to enter a number then calls the isPrime method to calculate if the number is prime or not. It then prints out to the screen the result. I did a lot of trial and error to get this code to work, but I don't really understand all the way why this program works. For instance if I enter 9, the code would give me back a remainder, which should return true, which should make 9 a prime number, but it's not and the program works, saying that 9 is not a prime number. Just wondering why it works.
package homework_chap5;
import java.util.Scanner;
public class Homework_Chap5 {
//Pg 313 #7
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
int num = s.nextInt();
if(isPrime(num)) {
System.out.println("Number is prime");
} else {
System.out.println("Number is not prime");
}
}
public static boolean isPrime(int num)
{
for(int i = 2; i <= num/2; i++)
{
if (num%i==0)
{
return false;
}
}
return true;
}
}
For instance if I enter 9, the code would give me back a remainder, which should return true
No, for num==9 and i==2, num%i==0 will be false, but the loop will proceed to check for i==3 whether num%i==0. This time the condition will be true and the isPrime method with return false.
Only after the loop ends, at which point we know that num is not divisible by any of the values of i tested, the method will conclude that num is a prime number and return true.
By definition, a number is prime if it is divisible by only 1 and itself. Also rather than checking divisibility of the number num by all the numbers between 1 and num, it is sufficient to check for divisibility by numbers between 1 and num/2 because no number greater than the mid value of the number num perfectly divides the number. So go from 2 till num/2 and see if any number divides the number num i.e. num % i == 0 where i in range [2, num/2]. If any such i exists then the number is not prime. If for no i in the range [2, num/2] num%i == 0 then num is prime.
However, the code can be optimized by checking for divisibility of num by all the numbers in range [2, sqrt(num)]instead of [2,num/2]. You can check the efficiency for your self.

Check whether a number is a sum of two prime numbers

The problem is to check a random number n can be the sum of 2 random prime numbers. For example,
if n=34 the possibilities can be (3+31), (5+29), (17+17)...
So far I have managed to save prime numbers to the array, but have no clue how I could check, if n is the sum of 2 prime numbers.
This is part of my code:
public static void primeNumbers(int n) {
int i = 0, candidate = 2, countArray = 0, countPrime = 0;
boolean flag = true;
while (candidate <= n) {
flag = true;
for (i = 2; i < candidate; i++) {
if ((candidate % i) == 0) {
flag = false;
break;
}
}
if (flag) {
countPrime++;
}
candidate++;
}
int[] primeNumbers = new int[countPrime];
while (candidate <= n) {
flag = true;
for (i = 2; i < candidate; i++) {
if ((candidate % i) == 0) {
flag = false;
break;
}
}
if (flag) {
primeNumbers[countArray] = candidate;
}
candidate++;
countArray++;
}
for (i = 0; i <= primeNumbers.length; i++) {
}
}
First I counted how many prime numbers are between 1-n so I can declare and initialize my array for prime numbers. Then I save prime numbers to the array. But now I have no idea how I could check if n is the sum of 2 prime numbers.
Given that you already have list of "prime numbers less than the given number", It is a very easy task to check if two prime numbers can sum to given number.
for(int i=0; i<array.length; i++){
int firstNum = array[i];
int secondNum = givenNum - firstNum;
/* Now if it is possible to sum up two prime nums to result into given num, secondNum should also be prime and be inside array */
if(ArrayUtils.contains(array, secondNum)){
System.out.println("Yes, it is possible. Numbers are "+ firstNum + " and " + secondNum);
}
}
EDIT: ArrayUtils is part of Apache Commons Lang library
You can however use ArrayList instead to use contains method.
You do not have to check all prime numbers from 1 to the given number or you don't even need an array.
Algorithm one can be;
First of all write a function that checks whether a given number is prime.
Split the number into two parts, 0 and the remaining value(the number itself). Now start decreasing the number part by 1 and start adding 1 to 0 simultaneously. Stop when the number part which we are decreasing becomes 0 or both the parts are prime numbers.
Another algorithm can be like this;(It is similar to the accepted answer)
Start subtracting prime numbers from the given number starting from 2.Check whether the subtraction is also prime.
The time you get the subtraction as a prime number, stop , you have got the two prime numbers that sum up to the given number.

printing Prime numbers in java

This Java code prints out prime numbers between 2-100.
And this works fine.
This code is not done by me.
But I am trying to figure out what is happening with this code.
Can anyone tell me what is happening after the second (for) loop?
class primenumbers{
public static void main(String args[])
{
int i, j;
boolean isprime;
for(i=2; i < 100; i++) {
isprime = true;
// see if the number is evenly divisible
for(j=2; j <= i/j; j++)
// if it is, then its not prime
if((i%j) == 0) isprime = false;
if(isprime)
System.out.println(i + " is prime.");
}
}
}
The first loop just for generating numbers from 2 to 100.
The second loop tries to find if the number is divisible by any other number. Here we try to divide a the number with a set of numbers (2 to prime_index).
Let's say the number is 10, the prime index is 10/2 = 5 for first iteration(j = 2). Which means, if the number 10 is not divisible by any number between 2 and 5, it's a prime number. It's divisible by 2 itself making it a non prime number.
Let's say the number is 9, now the prime index is 9/2 = 4 for first iteration(j = 2). Now, 9 % 2 gives 1 as reminder. So, loop continues for second iteration (j = 3). Now the prime index is 9/3 = 3 (Note here the prime index value is reduced from 4 to 3) So, now if the number is not divisible by 3, its decided as a prime number.
So, for every iteration, the prime index will reduce, making the number of iterations reduced.
Example for Number 97,
j = 2, prime index = 97/2 = 48 (no of iterations for loop)
j = 3, prime index = 97/3 = 32 (no of iterations for loop)
j = 4, prime index = 97/4 = 24 (no of iterations for loop)
j = 5, prime index = 97/5 = 19 (no of iterations for loop)
j = 6, prime index = 97/6 = 16 (no of iterations for loop)
j = 7, prime index = 97/7 = 13 (no of iterations for loop)
j = 8, prime index = 97/8 = 12 (no of iterations for loop)
j = 9, prime index = 97/9 = 10 (no of iterations for loop)
j = 10, prime index = 97/10 = 9 (loop exits as condition failed 10 <= 9 and declares 97 as a prime number)
Now, here the loop actually took 10 iterations instead of the proposed 48 iterations.
Let's modify the code for better understanding.
public static void main(String args[]) {
// Number from 2 to 100
for(int i=2; i < 100; i++) {
if (isPrimeNumber(i)) {
System.out.println(i + " is prime");
}
}
}
Now, lets see a method isPrimeNumberNotOptimized() which is not optimized.
private static boolean isPrimeNumberNotOptimized(int i) {
for(int j=2; j <= i/2; j++) {
// if it is, then its not prime
if((i%j) == 0) {
return false;
}
}
return true;
}
And, another method isPrimeNumberOptimized() which is optimized with prime index.
private static boolean isPrimeNumberOptimized(int i) {
for(int j=2; j <= i/j; j++) {
// if it is, then its not prime
if((i%j) == 0) {
return false;
}
}
return true;
}
Now, both methods will run and print the prime numbers correctly.
But, the optimized method will decide 97 is a prime number at 10th iteration.
And, the non-optimized method will decide the same in 48th iteration.
Hope, you understand it now.
FYI: prime index is the number we used for calculation. Such that if a number is not divisible between 2 & the derived prime index, its a prime number
This is a modification of the Sieve of Eratosthenes.
First, I'll just summarize what the sieve of Eratosthenes does, you can skip to the last paragraph if you already know this. The sieve of Eratosthenes algorithm loops through a certain boundary of numbers. (Say 2 - 100). For every number it loops through, it cancels out multiples, for example, since 2 is a prime number (true), all multiples of 2 are not (They are false). Same for 3, 5, and so on; and the algorithm skips every predetermined non-prime number. Therefore, a simulation of the algorithm would be:
2 ---prime number... cancels out 4, 6, 8, 10, 12....
3 ---prime number... cancels out 6, 9, 12, 15, 18...
4 --- ALREADY CANCELLED OUT... Skip
5 --- prime number... cancels out 10, 15, 20, 25...
6 --- ALREADY CANCELLED OUT... Skip
The numbers not cancelled are therefore the prime numbers. You can also read this for more information: http://www.geeksforgeeks.org/sieve-of-eratosthenes/
Now, to your question, your algorithm loops through the list of numbers. For each number(say x), it checks if any of the previous numbers looped through (i / j) is a factor of the x. The reason why i/j is used as the boundary for the second for loop is for efficiency. If you're checking if 10 (for example) is a prime number, there's no need to check whether 6 is a factor. You can conveniently stop at n/(n/2). That's what that part is trying to achieve.
Outer (first) loop enumerates all numbers in [2,100).
Inner (second) loop checks if a current number from the first loop is dividable by anything.
This check is done using % (remainder): (i%j)==0 when remainder of division i by j is 0. By definition when remainder is zero i is dividable by j and therefore is not a prime : isprime=false.
You only need to check in [2,i/j] because you only need to check up to sqrt(i) (explanation in the last section).
Having said that the inner loop can be re-written as:
...
int s = sqrt(i);
for(j=2; j <= s; j++)
...
however sqrt is more expensive than division, therefore it's better to rewrite j<=sqrt(i) as (squaring both sides) j^2 < i and j*j<i and j<i/j. When j is big enough j*j might overflow therefore both sides were divided by j in the final step.
Explanation of sqrt(i) taken from here: Why do we check up to the square root of a prime number to determine if it is prime?
if i is not prime, then some x exists so that i = x * j. If both x and j are greater than sqrt(i) then x*j would be greater than i.
Therefore at least one of those factors (x or j) must be less than or equal to the square root of i, and to check if i is prime, we only need to test for factors less than or equal to the square root.
public class PrimeNumber {
//check prime number
public static boolean prime(int number){
boolean isPrime=true;
for(int i=number-1;i>1;i--){
if(number%i==0){
isPrime=false;
System.out.println(i);
break;
}
}
return isPrime;
}
public static boolean getPrimeUsingWhile(int number){
boolean isPrime=true;
Integer temp=number-1;
while (temp>1){
if(number%temp==0){
isPrime=false;
break;
}
temp--;
}
return isPrime;
}
//print primenumber given length
public static List<Integer> prinPrimeList(){
boolean isPrime=true;
List<Integer> list=new ArrayList<>();
int temp=2;
while (list.size()<10){
for(int i=2;i<temp;i++){
if(temp%i==0){
isPrime=false;
break;
}else{
isPrime=true;
}
}
if(isPrime){list.add(temp);}
temp++;
}
return list;
}
public static void main(String arg[]){
System.out.println(prime(3));
System.out.println(getPrimeUsingWhile(5));
System.out.println(Arrays.toString(prinPrimeList().toArray()));
}
}

How to determine if a number is prime

Okay my issue is less of how to figure out if a number is prime, because I think I figured that out, but more of how to get it to display properly.
Here's my code:
public static void main(String[] args) {
// Declare Variables
int randomNumbers = 0;
int sum = 0;
//Loop for number generation and print out numbers
System.out.print("The five random numbers are: ");
for (int i = 0; i <= 4; i++)
{
randomNumbers = (int)(Math.random()*20);
sum += randomNumbers;
if (i == 4) {
System.out.println("and " + randomNumbers + ".");
}
else {
System.out.print(randomNumbers + ", ");
}
}
//Display Sum
System.out.println("\nThe sum of these five numbers is " + sum + ".\n");
//Determine if the sum is prime and display results
for(int p = 2; p < sum; p++) {
if(sum % p == 0)
System.out.println("The sum is not a prime number.");
else
System.out.println("The sum is a prime number.");
break;
}
}
}
Now my problem is, if the number ends up being something like 9, it'll say it is a prime number, which it is not. I think the issue is that the break is stopping it after one loop so it's not incrementing variable p so it's only testing dividing by 2 (I think). But if I remove the break point it will print out "The sum is/is not a prime number" on every pass until it exits the loop. Not sure what to do here.
Your method for finding if your number is prime is the correct method.
To make it so that it does not consistently print out whether or not the number is prime, you could have an external variable, which represents whether or not the number is prime.
Such as
boolean prime = true;
for (int p = 2; p < sum; p++) {
if (sum % p == 0) {
prime = false;
break;
}
}
if (prime)
System.out.println("The sum is a prime number.");
else
System.out.println("The sum is not a prime number.");
By doing this method the program will assume the number is prime until it proves that wrong. So when it finds it is not prime it sets the variable to false and breaks out of the loop.
Then after the loop finishes you just have to print whether or not the number was prime.
A way that you could make this loop faster is to go from when p = 2 to when p = the square root of sum. So using this method your for loop will look like this:
double sq = Math.sqrt((double)sum);
for (int p = 2; p < sq; p++) {
//Rest of code goes here
}
Hope this helps
You need to store whether or not the number is prime in a boolean outside of the loop:
//Determine if the sum is prime and display results
boolean isPrime = true;
for(int p = 2; p < sum; p++) {
if(sum % p == 0){
isPrime = false;
break;
}
}
if(isPrime){
System.out.println("The sum is a prime number.");
} else {
System.out.println("The sum is not a prime number.");
}
You are right, currently your code tests dividing by two, and the break command is stopping after one loop.
After the first go of your loop (p==2), the break will always stop the loop.
The fastest fix to your code will change the loop part like this:
boolean isPrime=true;
for(int p = 2; p < sum; p++) {
if(sum % p == 0) {
isPrime=false;
System.out.println("The sum is not a prime number.");
break;
}
}
if (isPrime)
System.out.println("The sum is a prime number.");
This code can be improved for efficiency and for code elegance.
For efficiency, you don't need to check divisibility by all numbers smaller than sum, it's enough to check all numbers smaller by square-root of sum.
For better code, create a seperate function to test if a number is prime.
Here is an example that implements both.
// tests if n is prime
public static boolean isPrime(int n) {
if (n<2) return false;
for(int p = 2; p < Math.sqrt(n); p++) {
if(n % p == 0) return false; // enough to find one devisor to show n is not a prime
}
return true; // no factors smaller than sqrt(n) were found
}
public static void main(String []args){
...
System.out.println("sum is "+ sum);
if (isPrime(sum))
System.out.println("The sum is a prime number.");
else
System.out.println("The sum is not a prime number.");
}
Small prime numbers
Use Apache Commons Math primality test, method is related to prime numbers in the range of int. You can find source code on GitHub.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
// org.apache.commons.math3.primes.Primes
Primes.isPrime(2147483629);
It uses the Miller-Rabin probabilistic test in such a way that a
result is guaranteed: it uses the firsts prime numbers as successive
base (see Handbook of applied cryptography by Menezes, table 4.1 / page 140).
Big prime numbers
If you are looking for primes larger than Integer.MAX_VALUE:
Use BigInteger#isProbablePrime(int certainty) to pre-verify the prime candidate
Returns true if this BigInteger is probably prime, false if it's
definitely composite. If certainty is ≤ 0, true is returned.
Parameters: certainty - a measure of the uncertainty that the caller
is willing to tolerate: if the call returns true the probability that
this BigInteger is prime exceeds (1 - 1/2certainty). The execution
time of this method is proportional to the value of this parameter.
Next use "AKS Primality Test" to check whether the candidate is indeed prime.
So many answers have been posted so far which are correct but none of them is optimized. That's why I thought to share the optimized code to determine prime number here with you. Please have a look at below code snippet...
private static boolean isPrime(int iNum) {
boolean bResult = true;
if (iNum <= 1 || iNum != 2 && iNum % 2 == 0) {
bResult = false;
} else {
int iSqrt = (int) Math.sqrt(iNum);
for (int i = 3; i < iSqrt; i += 2) {
if (iNum % i == 0) {
bResult = false;
break;
}
}
}
return bResult;
}
Benefits of above code-:
It'll work for negative numbers and 0 & 1 as well.
It'll run the for loop only for odd numbers.
It'll increment the for loop variable by 2 rather than 1.
It'll iterate the for loop only upto square root of number rather than upto number.
Explanation-:
I have mentioned the four points above which I'll explain one by one. Code must be appropriately written for the invalid inputs rather the only for valid input. Whatever answers have been written so far are restricted to valid range of input in which number iNum >=2.
We should be aware that only odd numbers can be prime, Note-: 2 is the only one even prime. So we must not run for loop for even numbers.
We must not run for loop for even values of it's variable i as we know that only even numbers can be devided by even number. I have already mentioned in the above point that only odd numbers can be prime except 2 as even. So no need to run code inside for loop for even values of variable i in for.
We should iterate for loop only upto square root of number rather than upto number. Few of the answers has implemented this point but still I thought to mention it here.
With most efficient time Complexity ie O(sqrt(n)) :
public static boolean isPrime(int num) {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
you can use the Java BigInteger class' isProbablePrime method to determine and print whether the sum is prime or not prime in an easy way.
BigInteger number = new BigInteger(sum);
if(number.isProbablePrime(1)){
System.out.println("prime");
}
else{
System.out.println("not prime");
}
You can read more about this method here https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#isProbablePrime%28int%29

Need help to find bug in factoring code

I am coding a program to factor a number into is prime factors. The program works as follows:
1) Input a number that you wish to factor (I will refer to is as "inputNumber")
2) Test to see if you can divide inputNumber by 2,3,5 and 7 (first 4 prime numbers). if you can divide by any of these, then do so as many times as you can (i.e. 12 can be divided by 2 twice, after this, there is a remainder of 3)
note every time I divide by a number, I store that number in an array list and I keep the remainder for further testing
3) Starting with i=11 (the next prime number) in a while loop, I do the following:
while (i < remainder+1) {
divides by i? yes: store i,
repeat until you cant divide by i. i=i+2,
divides by i? yes: store i, repeat until you can't divide by i.
i=i+4, same as before... i=i+2...
and finally stop the loop at i=i+2
}
this way, every successful iteration of the while loop will divide the remainder by numbers finishing by 1,3,7,9. We do not need to test even numbers since we divided by 2 already, nor do we need to test the ones ending by 5 since we already divided by 5. In the end, we
This is a very neat algorithm since it its much much faster than factoring a number by testing one number after the other, in fact, you only need to test 40% of all numbers.
My question is: When I tried factoring 84738329279, a number picked randomly, it omits to put the last prime factor in the list, I can't quite figure this out. the only factors that show up are 41, 61 and 61. Can someone help me find what I'm doing wrong? here is my code:
import java.util.Scanner;
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
// create a scanner object for inputs
Scanner in = new Scanner(System.in);
// prompt user for number to factor
System.out.print("enter a number to factor: ");
String digits = in.next();
BigInteger BigDigits = new BigInteger(digits);
BigInteger[] results = factor.factorThis(BigDigits);
System.out.print("Factors are ");
for (int i=0; i<results.length;i++){
System.out.print(results[i] + " ");
}
System.out.println("");
}
}
import java.util.ArrayList;
import java.math.BigInteger;
public class factor {
// Returns the prime factors of the BigInteger number
public static BigInteger[] factorThis(BigInteger number) {
BigInteger i = new BigInteger("11");
ArrayList<BigInteger> divisors = new ArrayList<BigInteger>(0);
BigInteger[] firstPrimes = new BigInteger[4];
firstPrimes[0] = new BigInteger("2");
firstPrimes[1] = new BigInteger("3");
firstPrimes[2] = new BigInteger("5");
firstPrimes[3] = new BigInteger("7");
// loop that test for first 4 prime numbers
for (int l=0;l<4;l++){
while ((number.mod(firstPrimes[l])).compareTo(BigInteger.ZERO) == 0) {
number = number.divide(firstPrimes[l]);
divisors.add(firstPrimes[l]);
}
}
// loop that factors only numbers finishing by 1,3,7,9
while (i.compareTo(number) == -1){
// check for ending by 1
if ((number.mod(i)).compareTo(BigInteger.ZERO) == 0) {
while (number.mod(i).compareTo(BigInteger.ZERO) == 0){
number = number.divide(i);
divisors.add(i);
}
}
else if ((number.mod(i)).compareTo(BigInteger.ZERO) != 0){
i=i.add(firstPrimes[0]);
}
// check for ending by 3
if ((number.mod(i)).compareTo(BigInteger.ZERO) == 0) {
while (number.mod(i).compareTo(BigInteger.ZERO) == 0){
number = number.divide(i);
divisors.add(i);
}
}
else if ((number.mod(i)).compareTo(BigInteger.ZERO) != 0){
i=i.add(firstPrimes[0].multiply(firstPrimes[0]));
}
//check for ending by 7
if ((number.mod(i)).compareTo(BigInteger.ZERO) == 0) {
while (number.mod(i).compareTo(BigInteger.ZERO) == 0){
number = number.divide(i);
divisors.add(i);
}
}
else if ((number.mod(i)).compareTo(BigInteger.ZERO) != 0){
i=i.add(firstPrimes[0]);
}
// check for ending by 9
if ((number.mod(i)).compareTo(BigInteger.ZERO) == 0) {
while (number.mod(i).compareTo(BigInteger.ZERO) == 0){
number = number.divide(i);
divisors.add(i);
}
}
else if ((number.mod(i)).compareTo(BigInteger.ZERO) != 0){
i=i.add(firstPrimes[0]);
}
}
// store prime factors into a BigInt array
String[] strArrayDivisors = divisors.toString().replaceAll("\\[", "").replaceAll("\\]","").replaceAll("\\s","").split(",");
BigInteger[] BigIntDivisors = new BigInteger[strArrayDivisors.length];
for(int j=0;j<strArrayDivisors.length;j++){
BigIntDivisors[j] = new BigInteger(strArrayDivisors[j]);
}
// returns all factors of "number"
return BigIntDivisors;
}
}
Thanks in advance.
First, 84738329279 = 41 * 61 * 61 * 555439. Your 41, 61, and 61 are correct.
But when your algorithm terminates, you are left with the last prime number still in number. You'll need to add code that tests number at the end: if it's 1, then you're already finished, else it needs to add it to divisors so it gets printed later.

Categories