prime number generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #16
    How about this pseudocode:

    Code:
    Loop X from START to END (We are testing X to see if it is prime):
       (Right now, X is prime as far as we know.)
       bool isPrime = true;
       Loop i from 2 to X (Don't include 1 or 0, because dividing by 0 gives error, and dividing by 1 always has no remainder)
          If X is evenly divisible by i
             X is not prime.
             Change isPrime to false, break from loop (no need to continue).
          Else
             Take no action (Can't be sure it will remain prime.)
       End Loop
       (Now, isPrime correctly indicates X's state).
       If X is prime
          Take appropriate action
       Else
          Take appropriate action
    End Loop

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #17
      Originally posted by Ganon11
      How about this pseudocode:

      Code:
      Loop X from START to END (We are testing X to see if it is prime):
         (Right now, X is prime as far as we know.)
         bool isPrime = true;
         Loop i from 2 to X (Don't include 1 or 0, because dividing by 0 gives error, and dividing by 1 always has no remainder)
            If X is evenly divisible by i
               X is not prime.
               Change isPrime to false, break from loop (no need to continue).
            Else
               Take no action (Can't be sure it will remain prime.)
         End Loop
         (Now, isPrime correctly indicates X's state).
         If X is prime
            Take appropriate action
         Else
            Take appropriate action
      End Loop
      Yes,that is nearly giving the corect output,if we by first else statement insert the
      next code:

      Code:
      				else{   if(x%3==0||x%5==0||x%7==0) break;
      					printf("\t%d",x);
      					delay(50);
      					isPrime=TRUE;
      					break;
                                      }
      ,and we before first loop:

      Code:
      printf("3\t5\t7");
      the output in range of 3-100 is corect.


      Savage.

      Comment

      • bluesteel
        New Member
        • Mar 2007
        • 84

        #18
        You are all wrong. The fact that a number can't be divided by two or three does not mean it is prime. 49 can't be divided by 2 nor 3 but it is not prime. If it can be divided by 2 then it is not prime. If it has square root it's not prime. The biggest number a number can divided by is not the square root.

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #19
          Originally posted by bluesteel
          You are all wrong. The fact that a number can't be divided by two or three does not mean it is prime. 49 can't be divided by 2 nor 3 but it is not prime. If it can be divided by 2 then it is not prime. If it has square root it's not prime. The biggest number a number can divided by is not the square root.
          And you have obviously read only part of the thread...

          Originally posted by ChaseCox
          A prime number is defined as a number that is only divisible evenly by itself and one. Meaning numbers like 1,2,3,5,7...41, 83...ect. To save on efficiency, you can eliminate all evens, except two, and any odd number that is evenly divible by 3,5,7,9,11...ec t. The resulting numbers should all be prime
          and again
          Originally posted by scruggsy
          A prime number is defined as a number that is only divisible evenly by itself and one. Meaning numbers like 1,2,3,5,7...41, 83...ect. To save on efficiency, you can eliminate all evens, except two, and any odd number that is evenly divible by 3,5,7,9,11...ec t. The resulting numbers should all be prime
          also
          Originally posted by Ganon11
          Not true. Consider the number 35. It's not divisible by 2 (35 / 2 = 17.5) or 3 (35 / 3 = 11.667), but it is divisible by 7 (35 / 7 = 5) and 5 (35 / 5 = 7). Thus, 35 is not prime - but your test would say it is.
          Only one or two people actually appeared to say what you claim about primes being any number not divisible by 2 or 3, and they were quickly corrected (see above).



          While it is true that the biggest number that divides a number is NOT the smaller than the square root, if a number does have any divisors, AT LEAST one of them MUST be less than the square root, so we only have to test to the square root.
          (think about 48, - 8 is larger than sqrt(48) (or approx 6.928). However, 8 * 6 = 48 and 6 is SMALLER than 6.928. Remember we are not looking for ALL factors, we are looking for ANY.....We know that there will always be the same number of factors less than the square root as greater than......so as long as we can show there are none less than, we know there couldn't be any greater than either....)

          Comment

          • bluesteel
            New Member
            • Mar 2007
            • 84

            #20
            I've thinking about this and i finally made a program able to list all the combinations possible to for a number by multiplications . It works perfectly.
            I am not going to post it now because i don't have the code here. If you want to ask me for it, send an email to me (<email removed>). I will send you a copy of it. Don't wait to see it here because i always forget addresses and might never enter this site again.

            Comment

            • bluesteel
              New Member
              • Mar 2007
              • 84

              #21
              Originally posted by DeMan
              And you have obviously read only part of the thread...


              and again


              also


              Only one or two people actually appeared to say what you claim about primes being any number not divisible by 2 or 3, and they were quickly corrected (see above).



              While it is true that the biggest number that divides a number is NOT the smaller than the square root, if a number does have any divisors, AT LEAST one of them MUST be less than the square root, so we only have to test to the square root.
              (think about 48, - 8 is larger than sqrt(48) (or approx 6.928). However, 8 * 6 = 48 and 6 is SMALLER than 6.928. Remember we are not looking for ALL factors, we are looking for ANY.....We know that there will always be the same number of factors less than the square root as greater than......so as long as we can show there are none less than, we know there couldn't be any greater than either....)
              I did know all that. That is what i took into consideration to develop my program. That is the way it works

              Comment

              • chella
                New Member
                • Mar 2007
                • 51

                #22
                Originally posted by Ganon11
                Not true. Consider the number 35. It's not divisible by 2 (35 / 2 = 17.5) or 3 (35 / 3 = 11.667), but it is divisible by 7 (35 / 7 = 5) and 5 (35 / 5 = 7). Thus, 35 is not prime - but your test would say it is.

                Hi,
                Try this code. It'll generate prime numbers from 2 to 100...

                the value 'a' here stores the limit i.e,upto 100 or 200.

                Code:
                #include<stdio.h>
                int main()
                {
                   int a,i;
                   int flag = 0;
                   for(a = 2 ; a <= 100 ;a++){
                      for (i = 2; i < a ;i++){
                         if (a % i == 0)
                            flag = 1;
                      }
                      if (flag == 0)
                         printf("%d\n",a);
                      flag =0;
                   }
                   return 0;
                }
                Please let me know if there is any issues.

                Regards,
                chella
                Last edited by Ganon11; Mar 5 '07, 12:46 PM. Reason: code tags added, indented

                Comment

                • DeMan
                  Top Contributor
                  • Nov 2006
                  • 1799

                  #23
                  That code sure will.....

                  Comment

                  • dryerlint
                    New Member
                    • Mar 2007
                    • 1

                    #24
                    ok... i tried that prime number generator and it worked! however....


                    when i attempted to change it slightly to make it to where anyone could put in any number and recieve back all the prime numbers up to that number, it screwed up on me. here is my changes (im bored and wanted to see if i could make it more useful to someone other than myself)


                    Code:
                    #include<stdio.h>
                    int main()
                    {
                    
                     int target;
                    
                     printf("What number would you like to see all the prime numbers to?");
                     scanf("%d", &target);
                    
                    int a,i;
                       int flag = 0;
                       for(a = 2 ; a <= 100 ;a++){
                          for (i = 2; i < a ;i++){
                             if (a % i == 0)
                                flag = 1;
                          }
                          if (flag == 0)
                             printf("%d\n",a);
                          flag =0;
                       }
                       return 0;
                    }

                    and these are the errors i recieved (using Vim in Unix)

                    primenumbergen. c: In function `main':
                    primenumbergen. c:12: parse error before `int'
                    primenumbergen. c:14: `a' undeclared (first use in this function)
                    primenumbergen. c:14: (Each undeclared identifier is reported only once
                    primenumbergen. c:14: for each function it appears in.)
                    primenumbergen. c:15: `i' undeclared (first use in this function)
                    primenumbergen. c:17: `flag' undeclared (first use in this function)

                    Comment

                    • chella
                      New Member
                      • Mar 2007
                      • 51

                      #25
                      Originally posted by dryerlint
                      ok... i tried that prime number generator and it worked! however....


                      when i attempted to change it slightly to make it to where anyone could put in any number and recieve back all the prime numbers up to that number, it screwed up on me. here is my changes (im bored and wanted to see if i could make it more useful to someone other than myself)


                      Code:
                      #include<stdio.h>
                      int main()
                      {
                      
                       int target;
                      
                       printf("What number would you like to see all the prime numbers to?");
                       scanf("%d", &target);
                      
                      int a,i;
                         int flag = 0;
                         for(a = 2 ; a <= 100 ;a++){
                            for (i = 2; i < a ;i++){
                               if (a % i == 0)
                                  flag = 1;
                            }
                            if (flag == 0)
                               printf("%d\n",a);
                            flag =0;
                         }
                         return 0;
                      }

                      and these are the errors i recieved (using Vim in Unix)

                      primenumbergen. c: In function `main':
                      primenumbergen. c:12: parse error before `int'
                      primenumbergen. c:14: `a' undeclared (first use in this function)
                      primenumbergen. c:14: (Each undeclared identifier is reported only once
                      primenumbergen. c:14: for each function it appears in.)
                      primenumbergen. c:15: `i' undeclared (first use in this function)
                      primenumbergen. c:17: `flag' undeclared (first use in this function)

                      Hi,
                      Declare the int a, i ... before the printf & scanf stmt's.
                      Hope this will work :-)

                      Comment

                      Working...