Connecting Tech Pros Worldwide Forums | Help | Site Map

prime numbers

Newbie
 
Join Date: Oct 2006
Posts: 17
#1: Oct 23 '06
Hello,I neead help with simple program in java.First test prime numbers with use array and second with use two method,thank.
Simi.

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 23 '06

re: prime numbers


Quote:

Originally Posted by ladislav

Hello,I neead help with simple program in java.First test prime numbers with use array and second with use two method,thank.
Simi.

Surely you should have some ideas of your own on how to do this. Why don't you post the ideas then others can add their own.
Newbie
 
Join Date: Oct 2006
Posts: 17
#3: Oct 23 '06

re: prime numbers


I need modify this code.

for(int pp=2;pp<100;pp++) //pp=possible prime
{
for(int pd=2;pd<pp;pd++) //pd=possible divisor
{
if(pp%pd==0)
break;
}
if(pp==pd)
System.out.println(pp);
}
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 23 '06

re: prime numbers


Quote:

Originally Posted by ladislav

I need modify this code.

for(int pp=2;pp<100;pp++) //pp=possible prime
{
for(int pd=2;pd<pp;pd++) //pd=possible divisor
{
if(pp%pd==0)
break;
}
if(pp==pd)
System.out.println(pp);
}

Expand|Select|Wrap|Line Numbers
  1. public class Primes {
  2.     public static void main(String[] args) {
  3.         int pp, pd = 0;
  4.         for(pp=2;pp<100;pp++) //pp=possible prime
  5.         {
  6.         for(pd=2;pd<pp;pd++) //pd=possible divisor
  7.         {
  8.         if(pp%pd==0)
  9.         break;
  10.         }
  11.         if(pp==pd)
  12.         System.out.println(pp);
  13.         }
  14.     }
  15. }
This will print all primes from 2 - 97. What exactly do you want to do?
Newbie
 
Join Date: Oct 2006
Posts: 17
#5: Oct 23 '06

re: prime numbers


I donīt know how pp replace by array obout 100 numbers?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Oct 23 '06

re: prime numbers


Quote:

Originally Posted by ladislav

I donīt know how pp replace by array obout 100 numbers?

Do you mean that you want to store the primes in an array of size 100? You would do it this way:

Expand|Select|Wrap|Line Numbers
  1. public class Primes {
  2.         public static void main(String[] args) {
  3.     int[] primes = new int[100];
  4.     int i = 0;
  5.     int pp, pd = 0;
  6.     for(pp=2;pp<100;pp++) //pp=possible prime
  7.     {
  8.          for(pd=2;pd<pp;pd++) //pd=possible divisor
  9.              {
  10.         if(pp%pd==0)
  11.         break;
  12.         }
  13.         if(pp==pd)
  14.         primes[i++] = pp;
  15.         }
  16.         for(int p: primes) {
  17.             System.out.println(p);
  18.         }
  19.  
  20.     }
  21. }
I'm sorry, but you are not being very clear
Newbie
 
Join Date: Oct 2006
Posts: 17
#7: Oct 23 '06

re: prime numbers


I am much obliged with the help of.
Reply