Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Blum Blum Shub

Blum Blum Shub

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
22 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Member_14956475

    Hi everyone . I am a newbie in programming , so i would be very grateful if you could help me . I have to implement the Blum Blum Shub in C or C++ using the stdlib.h and time.h libraries . Can anyone help me with this ? Thanks :)

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #13

    Member 14956475 wrote:

    I have to implement the Blum Blum Shub in C or C++...

    More importantly, can you do it with pencil and paper? If the answer is no, then: 1) you are going to be hard pressed to implement it in code, and 2) you should work on it until you can produce results on paper. Don't let computer code take the place of the basic understanding of algorithms.

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

    M 1 Reply Last reply
    0
    • D David Crow

      Member 14956475 wrote:

      I have to implement the Blum Blum Shub in C or C++...

      More importantly, can you do it with pencil and paper? If the answer is no, then: 1) you are going to be hard pressed to implement it in code, and 2) you should work on it until you can produce results on paper. Don't let computer code take the place of the basic understanding of algorithms.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      M Offline
      M Offline
      Member_14956475
      wrote on last edited by
      #14

      Okay , this is the code that i managed to do :

      #include
      #include
      #include
      /*BLUM BLUM SHUB*/

      int main()
      {

      const int p = 30000000091;
      const int q = 40000000003;
      int N ;
      scanf("%i", &N);
      const unsigned long long int s = 1200000003730000000273;
      unsigned int i,M,x[N];
      time_t t;
      srand((unsigned) time(&t));

      M=p*q;

      i=0;
      x[0]=s;
      for(i=1;i<=N;i++){
      x[i]=(x[i-1]*x[i-1])%M;
      }
      for(i=1;i<=N;i++){
      printf(" x[%d] = %d\n",i,x[i]);
      }

      system("PAUSE");
      return 0;
      }

      I have some issues with it , first it's that i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit . Also can anyone tell me how to fix the code as it generates the same numbers at some intervals , if you have other remarks on how to make it better tell me please . Thanks:)

      Richard Andrew x64R D 2 Replies Last reply
      0
      • M Member_14956475

        Okay , this is the code that i managed to do :

        #include
        #include
        #include
        /*BLUM BLUM SHUB*/

        int main()
        {

        const int p = 30000000091;
        const int q = 40000000003;
        int N ;
        scanf("%i", &N);
        const unsigned long long int s = 1200000003730000000273;
        unsigned int i,M,x[N];
        time_t t;
        srand((unsigned) time(&t));

        M=p*q;

        i=0;
        x[0]=s;
        for(i=1;i<=N;i++){
        x[i]=(x[i-1]*x[i-1])%M;
        }
        for(i=1;i<=N;i++){
        printf(" x[%d] = %d\n",i,x[i]);
        }

        system("PAUSE");
        return 0;
        }

        I have some issues with it , first it's that i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit . Also can anyone tell me how to fix the code as it generates the same numbers at some intervals , if you have other remarks on how to make it better tell me please . Thanks:)

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #15

        I found at least one issue with the code: You're using the variable N before you initialize it with a value. This is a no no.

        The difficult we do right away... ...the impossible takes slightly longer.

        1 Reply Last reply
        0
        • M Member_14956475

          Okay , this is the code that i managed to do :

          #include
          #include
          #include
          /*BLUM BLUM SHUB*/

          int main()
          {

          const int p = 30000000091;
          const int q = 40000000003;
          int N ;
          scanf("%i", &N);
          const unsigned long long int s = 1200000003730000000273;
          unsigned int i,M,x[N];
          time_t t;
          srand((unsigned) time(&t));

          M=p*q;

          i=0;
          x[0]=s;
          for(i=1;i<=N;i++){
          x[i]=(x[i-1]*x[i-1])%M;
          }
          for(i=1;i<=N;i++){
          printf(" x[%d] = %d\n",i,x[i]);
          }

          system("PAUSE");
          return 0;
          }

          I have some issues with it , first it's that i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit . Also can anyone tell me how to fix the code as it generates the same numbers at some intervals , if you have other remarks on how to make it better tell me please . Thanks:)

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #16

          Member 14956475 wrote:

          ...i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit .

          Change your main() signature to:

          void main( int argc, char *argv[] )

          Then access argv[1] to get D as a command line argument. Also, your two for() loops are accessing beyond the end of the x array. In C, arrays start at 0.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          M 1 Reply Last reply
          0
          • D David Crow

            Member 14956475 wrote:

            ...i need to add from console the biggest number D and i dont know how to put it in as now it's generating numbers without upper limit .

            Change your main() signature to:

            void main( int argc, char *argv[] )

            Then access argv[1] to get D as a command line argument. Also, your two for() loops are accessing beyond the end of the x array. In C, arrays start at 0.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            M Offline
            M Offline
            Member_14956475
            wrote on last edited by
            #17

            I've replaced main() with

            void main( int argc, char *argv[] )

            , but how can i acces argv[1] to get D as a command line argument?

            D 1 Reply Last reply
            0
            • M Member_14956475

              I've replaced main() with

              void main( int argc, char *argv[] )

              , but how can i acces argv[1] to get D as a command line argument?

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #18

              Member 14956475 wrote:

              ...but how can i acces argv[1] to get D as a command line argument?

              The same way you would access parameters from within any other function. main() is no different. See here for more.

              Member 14956475 wrote:

              ...is it possible to use srand command?

              Your code is already doing that. However, the way in which you are using it is meaningless since you are also not calling rand(). rand() by itself will always generate the same sequence of numbers. srand() will "seed" the number generation process to start at a different spot.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              M 1 Reply Last reply
              0
              • D David Crow

                Member 14956475 wrote:

                ...but how can i acces argv[1] to get D as a command line argument?

                The same way you would access parameters from within any other function. main() is no different. See here for more.

                Member 14956475 wrote:

                ...is it possible to use srand command?

                Your code is already doing that. However, the way in which you are using it is meaningless since you are also not calling rand(). rand() by itself will always generate the same sequence of numbers. srand() will "seed" the number generation process to start at a different spot.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                M Offline
                M Offline
                Member_14956475
                wrote on last edited by
                #19

                #include #include #include /*BLUM BLUM SHUB*/ int main () { const int p = 347; const int q = 351; const unsigned long long int s = 12373273; int N ; printf("enter the number:"); scanf("%i", &N); unsigned int i,M,x[N]; srand(time(NULL)); M=p*q; i=0; x[0]=s; for(i=1;i<=N;i++){ x[i]=rand()*(x[i-1]*x[i-1])%M; } for(i=1;i<=N;i++){ printf("%d\t",x[i]); } system("PAUSE"); return 0; } I added srand and rand is it okay working now ?

                D 1 Reply Last reply
                0
                • M Member_14956475

                  #include #include #include /*BLUM BLUM SHUB*/ int main () { const int p = 347; const int q = 351; const unsigned long long int s = 12373273; int N ; printf("enter the number:"); scanf("%i", &N); unsigned int i,M,x[N]; srand(time(NULL)); M=p*q; i=0; x[0]=s; for(i=1;i<=N;i++){ x[i]=rand()*(x[i-1]*x[i-1])%M; } for(i=1;i<=N;i++){ printf("%d\t",x[i]); } system("PAUSE"); return 0; } I added srand and rand is it okay working now ?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #20

                  Member 14956475 wrote:

                  is it okay working now ?

                  Only you can determine that.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  1 Reply Last reply
                  0
                  • M Member_14956475

                    Hi everyone . I am a newbie in programming , so i would be very grateful if you could help me . I have to implement the Blum Blum Shub in C or C++ using the stdlib.h and time.h libraries . Can anyone help me with this ? Thanks :)

                    M Offline
                    M Offline
                    Member_14956475
                    wrote on last edited by
                    #21

                    Okay , so this is the final code that i have at the moment #include #include #include /*BLUM BLUM SHUB*/ int main () { const int p = 347; const int q = 351; const unsigned long long int s = 12373273; int N ; printf("Introdu numarul de cifre aleatorii:"); scanf("%i", &N); unsigned int i,M,x[N]; srand(time(NULL)); M=p*q; i=0; x[0]=s; FILE *fp; //txt code fp = fopen("file.txt", "w"); //txt code for(i=1;i<=N;i++){ x[i]=rand()*(x[i-1]*x[i-1])%M; } for(i=1;i<=N;i++){ printf("%d\t",x[i]); fprintf(fp, "%d ",x[i]); //txt code } system("PAUSE"); fclose(fp);//txt code return 0; } It's working but i have one issue that i can't still solve , i need to put a maximal limit of the generated numbers at input , can anyone give me a clue how i can set x[i] a limit? I only found how to put a limit at rand but when it multiplies with function of BBS it exceeds the limit

                    L 1 Reply Last reply
                    0
                    • M Member_14956475

                      Okay , so this is the final code that i have at the moment #include #include #include /*BLUM BLUM SHUB*/ int main () { const int p = 347; const int q = 351; const unsigned long long int s = 12373273; int N ; printf("Introdu numarul de cifre aleatorii:"); scanf("%i", &N); unsigned int i,M,x[N]; srand(time(NULL)); M=p*q; i=0; x[0]=s; FILE *fp; //txt code fp = fopen("file.txt", "w"); //txt code for(i=1;i<=N;i++){ x[i]=rand()*(x[i-1]*x[i-1])%M; } for(i=1;i<=N;i++){ printf("%d\t",x[i]); fprintf(fp, "%d ",x[i]); //txt code } system("PAUSE"); fclose(fp);//txt code return 0; } It's working but i have one issue that i can't still solve , i need to put a maximal limit of the generated numbers at input , can anyone give me a clue how i can set x[i] a limit? I only found how to put a limit at rand but when it multiplies with function of BBS it exceeds the limit

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #22

                      That should already work, but you can add another set of parenthesis to make sure:

                      x[i] = (rand() * (x[i-1] * x[i-1])) % M;

                      So the random number times the square of x[i-1] should be calculated first.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups