Storing and doing modulus on long doubles

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zacksoniar
    New Member
    • Sep 2007
    • 45

    Storing and doing modulus on long doubles

    hello guys,

    I'm implementing RSA algo in which i need to perform very huge integer power,mod functions. 'int' is having small range so i decided to go for long double. but,I am not able to use it.

    can anybody plz tell me code to do following tasks:

    take long double 26 & 53.

    calculate 26^53 using pow fun.

    calculate mod: (26^53)%250.

    answer should be 76.

    i'm not getting answer, Plz help.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by zacksoniar
    hello guys,

    I'm implementing RSA algo in which i need to perform very huge integer power,mod functions. 'int' is having small range so i decided to go for long double. but,I am not able to use it.

    can anybody plz tell me code to do following tasks:

    take long double 26 & 53.

    calculate 26^53 using pow fun.

    calculate mod: (26^53)%250.

    answer should be 76.

    i'm not getting answer, Plz help.
    Please post what you have tried first.

    Comment

    • zacksoniar
      New Member
      • Sep 2007
      • 45

      #3
      long double a,b,c,d;
      a=26;
      b=53;

      c=pow(a,b); //inbuilt pow fun.

      //following 3 lines compute mod for long double.

      d=c/250; //quotient
      modfl(d,&d); //collectint integral part
      c=c-(d*250); //get reminder i.e. mod(maths formula)

      printf("%Lf",c) //c should be 76.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        For the modulo thing you don't need extremely large numbers; the remainders
        (mod 250) for the first powers of 26 are 26, 176, 76, 226, 126; further powers
        of 26 (mod 250) repeat this pattern; therefore 26^53 mod 250 == 26^3 mod 250 == 76.

        kind regards,

        Jos

        Comment

        • zacksoniar
          New Member
          • Sep 2007
          • 45

          #5
          friend,

          i have given that example for understanding purpose to tell how i have done coding. those nos 26,53 & 250 are subject to change according to need.

          what my question was how to use long double type because using that type, i'm not getting answers for large nos. such as
          (26^53)mod 250.(answer should be 76).

          i need code that calculates result for similar stuff. eg
          (27^45)mod300 using long double.


          thank u.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Hold on a minute.

            d is c/250.

            Therefore, d* 250 is c.

            Therefore, c-(d*250) is really c - c which is 0.

            What's the problem again?

            Comment

            • zacksoniar
              New Member
              • Sep 2007
              • 45

              #7
              guys,

              d=c/250 is true but because it is long double so it gives qoutient(d) with fraction.

              then i used 'modfl' function so that i get only integral part of d.

              thus, c-(d*250) is not equal to zero. it gives mod.
              because if c is divisible by 250 then it can be written as
              c=qoutient*250+ c mod 250
              where,qoutient= c/250 & mod is what i want.

              u guys are not helping out.

              what my question is with values like 26 & 53. long double data type should give the answer of problem (26^53)mod250. but it gives 0.00000.

              plz help me out. I have exam tomorrow.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by zacksoniar
                friend,

                i have given that example for understanding purpose to tell how i have done coding. those nos 26,53 & 250 are subject to change according to need.

                what my question was how to use long double type because using that type, i'm not getting answers for large nos. such as
                (26^53)mod 250.(answer should be 76).

                i need code that calculates result for similar stuff. eg
                (27^45)mod300 using long double.


                thank u.
                You can't use long doubles for that; the result of 27^45 ==
                257851336715142 813961161489479 091783218382487 523072645055950 53707
                which contains far too many digits for a long double.

                Do the modulo trick I sketched instead:
                Multiply 27 by itself modulo 300 until it starts to repeat itself:

                27, 129, 183, 141, 207, 189, 3, 81, 87, 249, 123, 21, 267, 9, 243, 261, 147, 69, 63, 201, 27

                The result (mod 300) of 27^21 is the same as 27^1 (mod 300) so the result of
                27^45 (mod 300) is the same as 27^5 (mod 300) which is 207.

                kind regards,

                Jos

                Comment

                Working...