# Help with prime and composite numbers

Page 1 of 1

## 8 Replies - 17866 Views - Last Post: 07 June 2010 - 08:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=64341&amp;s=858baa9591b6c21879eb08b225053478&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sucklefish

Reputation: 0
• Posts: 11
• Joined: 23-August 08

# Help with prime and composite numbers

Posted 18 September 2008 - 05:16 PM

So I have a way to determine of a number is prime or composite and it works fine but its not exactly the best code...

```double[] primeNums = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
```

```for (int scan = 0; scan < primeNums.length; scan++) // scans array of primeNums[]
{
if (d == primeNums[scan]) prime = "Prime"; // if it finds a prime
else prime = "Composite"; // if it doesnt find a prime
if ( ( d % 2 ) != 0) prime = "Prime"; // if the number is a decimal
```

I need something that can determine if it is prime or composite with out listing them out.

my brain is so fried right now I have no idea where to even start. ANY help would be greatly appreciated.

Also If anyone has a good tut for getting rid of one giant public into smaller privates I also have no idea how to do that

This post has been edited by Sucklefish: 18 September 2008 - 05:17 PM

Is This A Good Question/Topic? 0

## Replies To: Help with prime and composite numbers

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Help with prime and composite numbers

Posted 18 September 2008 - 05:39 PM

There are dozen of algorithms to compute prime numbers. If you search this foreum for "prime number" you will find multiple implementations.

In your case a quick dirty trick would do it.... (by the way you declare your primes as double, they should be int) but I will use a boolean

```boolean[] isPrime = new boolean[10000];	// init first 10000 at false

for(i  = 1; i < isPrime.length; i += 2)	// put all the odd one to true
isPrime[i] = true;

for(int i = 3; i < isPrime.length; i += 2 {	// no need to test the even one
for(j = 3; j < i; j += 2) {
if(i % j == 0) {
isPrime[i] = false;
break;
}
}
}

```

### #3 BetaWar

• #include "soul.h"

Reputation: 1513
• Posts: 8,290
• Joined: 07-September 06

## Re: Help with prime and composite numbers

Posted 18 September 2008 - 05:54 PM

Here is a quick app that will find if a number is prime, and if it isn't what all the components that create the number are:
```import java.util.Scanner;

public class PrimeFinder{
private static String comps;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number = input.nextInt();
comps = "";
boolean prime = isPrime(number);
if(prime){
System.out.println("The number "+number+" is Prime");
}
else{
System.out.println("The number "+number+" is composed of "+comps);
}
}
public static boolean isPrime(int num){
boolean isTrue = true;
for(int i=2; i<=Math.ceil((num/2)); i++){
if(num%i == 0){
if(comps.equals("")){
comps += ""+i;
}
else{
comps += ","+i;
}
isTrue = false;
}
}
return isTrue;
}
}
```

If you wanted to find out what half of the components are you could say
```import java.util.Scanner;

public class PrimeFinder{
private static String comps;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number = input.nextInt();
comps = "";
boolean prime = isPrime(number);
if(prime){
System.out.println("The number "+number+" is Prime");
}
else{
System.out.println("The number "+number+" is composed of "+comps);
}
}
public static boolean isPrime(int num){
boolean isTrue = true;
for(int i=2; i<=Math.ceil(Math.sqrt(num)); i++){
if(num%i == 0){
if(comps.equals("")){
comps += ""+i;
}
else{
comps += ","+i;
}
isTrue = false;
}
}
return isTrue;
}
}
```

Instead. NOTICE - it uses the square root function instead of dividing by 2.

Hope that helps.

### #4 Sucklefish

Reputation: 0
• Posts: 11
• Joined: 23-August 08

## Re: Help with prime and composite numbers

Posted 18 September 2008 - 07:55 PM

Well I basically feel like a complete idiot right now. I tried both of your examples trying to adapt it to my code but my head is just not clear right now.

```/**
* @(#)numbergame.java
*
* numbergame application
*
* @Kevin Ryan
* @version 1.00 2008/9/5
*/
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
public class numbergame
{
public static void main(String[] args)
{
int play, error = 0, playagain; // declares play and error which prompt the start box and whether or not there were errors
double[] primeNums = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
String s2 = "0", s = "0", even = "", prime = ""; // declares strings for prime, even, and the input string before and after trim
double d = 0 , squareroot, squared; // declares double for d which is the value holder, squareroot, and squared.

play = JOptionPane.showConfirmDialog (null,"Would you like to play a game?"); // prompts to start game
if (play == JOptionPane.YES_OPTION) // if the user presses yes
if (play == JOptionPane.CANCEL_OPTION)
System.exit(0);
if (play == JOptionPane.NO_OPTION)
System.exit(0);
do{
{
s = JOptionPane.showInputDialog ("Enter a number between 1 and 100\nand click ok"); // input box
s2 = s.trim(); // trims answer
}

try {
d = Double.parseDouble(s2); // tries to parse into a double
}
catch(NumberFormatException ex) // if it can't parse it
{
error = 1; // throws an error
JOptionPane.showMessageDialog(null, "You entered an invalid value.\nSystem Critical!\nShuting down...", "!!!", JOptionPane.ERROR_MESSAGE); // tells them they entered an invalid value
if (error == 1) System.exit(0); // closes program if errors == 1
}
if (d < 0 || d > 100) JOptionPane.showMessageDialog(null, "You entered a value that is out of range.\nSystem Critical!\nShuting down...", "!!!", JOptionPane.ERROR_MESSAGE); // if the number is too big
if (d < 0 || d > 100) System.exit(0); // if the number is too big
if ( ( d % 2 ) == 0) even = "even"; // if its even
if ( ( d % 2 ) != 0) even = "odd"; // if its odd

for (int scan = 0; scan < primeNums.length; scan++) // scans array of primeNums[]
{
if (d == primeNums[scan]) prime = "Prime"; // if it finds a prime
else if (d != primeNums[scan]) prime = "Composite"; // if it doesnt find a prime
}

squareroot = Math.sqrt(d); // gets the squareroot
squared = d * d; // squares it
// Displays output.
if (d >= 0 || d <= 100) JOptionPane.showMessageDialog(null, "You entered: " + d + "\n" + d + " is " + even + "\nThe squareroot of " + d + " is: " + squareroot + "\nThe square of " + d + " is " + squared + "\nThat number is a " + prime + " number");
playagain = JOptionPane.showConfirmDialog(null, "Would you like to play again?");
}
while (playagain == JOptionPane.YES_OPTION);

}
}

```

I realized that the code i had
``` if ( ( d % 2 ) != 0) prime = "Prime"; // if the number is a decimal
```

was taking out all odd numbers, not sure how i didn't catch that before.

I'm not even sure as to what to do at this point. I can't turn it in as is considering it doesn't even work at all now.

Gah I hate getting this frustrated it makes it even more impossible to think

Reputation:

## Re: Help with prime and composite numbers

Posted 07 June 2010 - 05:21 AM

Sucklefish, on 18 September 2008 - 04:16 PM, said:

So I have a way to determine of a number is prime or composite and it works fine but its not exactly the best code...

```double[] primeNums = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
```

```for (int scan = 0; scan < primeNums.length; scan++) // scans array of primeNums[]
{
if (d == primeNums[scan]) prime = "Prime"; // if it finds a prime
else prime = "Composite"; // if it doesnt find a prime
if ( ( d % 2 ) != 0) prime = "Prime"; // if the number is a decimal
```

I need something that can determine if it is prime or composite with out listing them out.

my brain is so fried right now I have no idea where to even start. ANY help would be greatly appreciated.

Also If anyone has a good tut for getting rid of one giant public into smaller privates I also have no idea how to do that

Reputation:

## Re: Help with prime and composite numbers

Posted 07 June 2010 - 05:26 AM

i think you should put a loop ordered by 2,3,5,7.. modulo the number that is to be determine wether it is prime or composite by this 4 numbers...

example the number is 8 then you modulo(%) it by 2 it will automatically result in 0 meaning if the modulo is equal to zero the result will be "Composite" then if it is not 0 the result is "Prime"

lets take a next example.. 25

25%2 is 1 but it is a loop therefore it will go upwards on 3
25%3 is 0 then end the loop if there is a 0 result less than the number youve inputed 25-1

### #7 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: Help with prime and composite numbers

Posted 07 June 2010 - 05:28 AM

@Zaine
This post is about 2 years old... I don't think the op is likely to check for more updates any time soon...

### #8 webpeater

Reputation: 26
• Posts: 95
• Joined: 04-June 10

## Re: Help with prime and composite numbers

Posted 07 June 2010 - 05:31 AM

zaine, on 07 June 2010 - 12:26 PM, said:

i think you should put a loop ordered by 2,3,5,7.. modulo the number that is to be determine wether it is prime or composite by this 4 numbers...

example the number is 8 then you modulo(%) it by 2 it will automatically result in 0 meaning if the modulo is equal to zero the result will be "Composite" then if it is not 0 the result is "Prime"

lets take a next example.. 25

25%2 is 1 but it is a loop therefore it will go upwards on 3
25%3 is 0 then end the loop if there is a 0 result less than the number youve inputed 25-1

I used this moment in a project of mine, some time ago :

```	/**
* Check if the given number is prime
*
* @param	number
* 			The number to be checked
* @return	Whether or not the given number is prime
*/
public static boolean isPrime(long number){
BigInteger bi = BigInteger.valueOf(number);
if(bi.isProbablePrime(10)) {
for(long i = 2; i < Math.sqrt(number); i++) {
if (number%i == 0)
return false;
}
return true;
}
return false;
}
```

EDIT : Ow, didn't notice this post was so old

This post has been edited by webpeater: 07 June 2010 - 05:35 AM

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12318
• Posts: 45,417
• Joined: 27-December 08

## Re: Help with prime and composite numbers

Posted 07 June 2010 - 08:00 PM

Thread opened, as there are useful solutions that may help other users in the future. Before closing necro'd threads, please keep in mind that you will be removing this solution from search engine results.