prime numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ladislav
    New Member
    • Oct 2006
    • 17

    #1

    prime numbers

    Hello,I neead help with simple program in java.First test prime numbers with use array and second with use two method,thank.
    Simi.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • ladislav
      New Member
      • Oct 2006
      • 17

      #3
      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.prin tln(pp);
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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.prin tln(pp);
        }
        Code:
        public class Primes {
        	public static void main(String[] args) {
        		int pp, pd = 0;
        		for(pp=2;pp<100;pp++) //pp=possible prime
        		{
        		for(pd=2;pd<pp;pd++) //pd=possible divisor
        		{
        		if(pp%pd==0)
        		break;
        		}
        		if(pp==pd)
        		System.out.println(pp);
        		}
        	}
        }
        This will print all primes from 2 - 97. What exactly do you want to do?

        Comment

        • ladislav
          New Member
          • Oct 2006
          • 17

          #5
          I don´t know how pp replace by array obout 100 numbers?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            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:

            Code:
            public class Primes {
                    public static void main(String[] args) {
            	int[] primes = new int[100];
            	int i = 0;
            	int pp, pd = 0;
            	for(pp=2;pp<100;pp++) //pp=possible prime
            	{
             	    for(pd=2;pd<pp;pd++) //pd=possible divisor
            	         {
            		if(pp%pd==0)
            		break;
            		}
            		if(pp==pd)
            		primes[i++] = pp;
            		}
            		for(int p: primes) {
            			System.out.println(p);
            		}
            
            	}
            }
            I'm sorry, but you are not being very clear

            Comment

            • ladislav
              New Member
              • Oct 2006
              • 17

              #7
              I am much obliged with the help of.

              Comment

              Working...