loop for finding possible odd divisors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #76
    Originally posted by scienceman88
    is there a way I could define a data type that will fit all data under 171 undecillion ?
    As I wrote before: you need a 'big integer' package; several are available, Google is your friend. About the definition of S(n): if it is what I think it is it's the sum of all proper divisors of n except n itself, e.g. S(6) == 1+2+3 = 6; S(28) == 1+2+4+7+14 == 28. When you see large primes you see perfect numbers around the corner (a perfect number equals the sum of its divisors, i.e. n == S(n)).

    kind regards,

    Jos

    Comment

    • scienceman88
      New Member
      • Aug 2009
      • 85

      #77
      could a code be added to check those if so we would fit all needs for a mersenne prime tester and I know what else to change to find a new record faster.

      Comment

      • scienceman88
        New Member
        • Aug 2009
        • 85

        #78
        found a full code for mersennes on rosettacode.org if it's true I can whip Gimps want to try ?

        Comment

        • scienceman88
          New Member
          • Aug 2009
          • 85

          #79
          Code:
          #include <math.h>
          #include <stdio.h>
          #include <limits.h>
          #pragma precision=log10l(ULLONG_MAX)/2
          
          typedef enum { FALSE=0, TRUE=1 } BOOL;
          
          BOOL is_prime( int p ){
            if( p == 2 ) return TRUE;
            else if( p <= 1 || p % 2 == 0 ) return FALSE;
            else {
          	BOOL prime = TRUE;
          	const int to = sqrt(p);
          	int i;
              for(i = 3; i <= to; i+=2)
          	  if (!(prime = p % i))break;
          	return prime;
            }
          }
          
          BOOL is_mersenne_prime( int p ){
            if( p == 2 ) return TRUE;
            else {
          	const long long unsigned m_p = ( 1LLU << p ) - 1;
          	long long unsigned s = 4;
          	int i;
          	for (i = 3; i <= p; i++){
          	  s = (s * s - 2) % m_p;
              }
          	return s == 0;
            }
          }
          
          int main(int argc, char **argv){
          
            const int upb = log2l(ULLONG_MAX)/2;
            int p;
            printf(" Mersenne primes:\n");
            for( p = 43112609; p <= upb; p += 1 ){
          	if( is_prime(p) && is_mersenne_prime(p) ){
          	  printf (" M%u",p);
          	}
            }
            printf("\n");
          }
          is an edited version of the code on rosettacode.org it gives me these warning can you help me solve them ?


          [BCC32 Warning] File2.c(36): W8065 Call to function 'log2l' with no prototype
          [BCC32 Warning] File2.c(45): W8070 Function should return a value
          [ILINK32 Error] Error: Unresolved external '_log2l' referenced from C:\USERS\RODDY\ DOCUMENTS\RAD STUDIO\PROJECTS \DEBUG\FILE2.OB J
          Last edited by JosAH; Aug 17 '09, 01:22 PM. Reason: added [code] ... [/code] tags

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #80
            Originally posted by scienceman88
            [BCC32 Warning] File2.c(36): W8065 Call to function 'log2l' with no prototype
            [BCC32 Warning] File2.c(45): W8070 Function should return a value
            [ILINK32 Error] Error: Unresolved external '_log2l' referenced from C:\USERS\RODDY\ DOCUMENTS\RAD STUDIO\PROJECTS \DEBUG\FILE2.OB J
            There is no prototype of function log2l( ... ) in scope anywhere; check your manual for which header file includes that prototype; #include that header file.

            Your function main( ... ) is supposed to return an int. You are not returning anything at the end of that function. Insert 'return 0;' there.

            Those old systems want you to explicitly link in the math library; add the '-lm' flag on the linker's command line.

            kind regards,

            Jos

            Comment

            • scienceman88
              New Member
              • Aug 2009
              • 85

              #81
              Originally posted by JosAH
              There is no prototype of function log2l( ... ) in scope anywhere; check your manual for which header file includes that prototype; #include that header file.

              Your function main( ... ) is supposed to return an int. You are not returning anything at the end of that function. Insert 'return 0;' there.

              Those old systems want you to explicitly link in the math library; add the '-lm' flag on the linker's command line.

              kind regards,

              Jos
              lm flag ? god I suck at C
              even I figured the return part
              I go by the standard headers and i have no idea what one would have it maybe it's a C99 complex.h function.

              Comment

              • scienceman88
                New Member
                • Aug 2009
                • 85

                #82
                Think if my search is right it's in math.h but I could be wrong http://www.codecogs.com/reference/c/...hp?alias=log2l

                Comment

                • scienceman88
                  New Member
                  • Aug 2009
                  • 85

                  #83
                  I didn't even know there was a search.h see I learn things too much so I forget most things I think I love learning though so I'll have to adapt

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #84
                    Originally posted by scienceman88
                    lm flag ? god I suck at C
                    even I figured the return part
                    I go by the standard headers and i have no idea what one would have it maybe it's a C99 complex.h function.
                    I don't know what you mean by 'Im flag ?'. The log2l( ... ) function most likely is a Borland function so they must have a header file available for it and, more important, a library that contains the definition of it. It is not a C99 thing.

                    kind regards,

                    Jos

                    Comment

                    • scienceman88
                      New Member
                      • Aug 2009
                      • 85

                      #85
                      by what I've found it should be in math.h so I have no idea why it doesn't work I know what its supposed to do but I don't know how I can do it

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #86
                        Originally posted by scienceman88
                        by what I've found it should be in math.h so I have no idea why it doesn't work I know what its supposed to do but I don't know how I can do it
                        Check your math.h file and see if you can find a prototype of that function in there.

                        kind regards,

                        Jos

                        Comment

                        • scienceman88
                          New Member
                          • Aug 2009
                          • 85

                          #87
                          Works with: gcc version 4.1.2 20070925 (Red Hat 4.1.2-27) Works with: C99 Compiler options: gcc -std=c99 -lm Lucas-Lehmer_test.c -o Lucas-Lehmer_test
                          #include <math.h>
                          #include <stdio.h>
                          #include <limits.h>
                          #pragma precision=log10 l(ULLONG_MAX)/2

                          typedef enum { FALSE=0, TRUE=1 } BOOL;

                          BOOL is_prime( int p ){
                          if( p == 2 ) return TRUE;
                          else if( p <= 1 || p % 2 == 0 ) return FALSE;
                          else {
                          BOOL prime = TRUE;
                          const int to = sqrt(p);
                          int i;
                          for(i = 3; i <= to; i+=2)
                          if (!(prime = p % i))break;
                          return prime;
                          }
                          }

                          BOOL is_mersenne_pri me( int p ){
                          if( p == 2 ) return TRUE;
                          else {
                          const long long unsigned m_p = ( 1LLU << p ) - 1;
                          long long unsigned s = 4;
                          int i;
                          for (i = 3; i <= p; i++){
                          s = (s * s - 2) % m_p;
                          }
                          return s == 0;
                          }
                          }

                          int main(int argc, char **argv){

                          const int upb = log2l(ULLONG_MA X)/2;
                          int p;
                          printf(" Mersenne primes:\n");
                          for( p = 2; p <= upb; p += 1 ){
                          if( is_prime(p) && is_mersenne_pri me(p) ){
                          printf (" M%u",p);
                          }
                          }
                          printf("\n");
                          }
                          Output:
                          Mersenne primes:
                          M2 M3 M5 M7 M13 M17 M19 M31

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #88
                            Originally posted by scienceman88
                            Works with: gcc version 4.1.2 20070925 (Red Hat 4.1.2-27) Works with: C99 Compiler options: gcc -std=c99 -lm Lucas-Lehmer_test.c -o Lucas-Lehmer_test

                            [ ... ]

                            Output:
                            Mersenne primes:
                            M2 M3 M5 M7 M13 M17 M19 M31
                            So it works for small values of p; now go for the really big ones. btw, please use those code tags around your code for readability reasons.

                            kind regards,

                            Jos

                            Comment

                            • scienceman88
                              New Member
                              • Aug 2009
                              • 85

                              #89
                              I can't get it to work for any

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                #90
                                Originally posted by scienceman88
                                I can't get it to work for any
                                You have to be more exact: first you write that the output happened to be correct and showed it to us and now you're writing that it doesn't work at all. I you want help you have to help us understand you. btw, the probability of a thread ending successfully is inversely proportional to the length of the thread.

                                kind regards,

                                Jos

                                Comment

                                Working...