Results 1 to 3 of 3

Math Help - What's wrong in this code ?

  1. #1
    Super Member Bacterius's Avatar
    Joined
    Nov 2009
    From
    Wellington
    Posts
    927

    What's wrong in this code ?

    Hey all,
    I've been trying to figure out for a whole evening what was wrong in the code I wrote to apply the extended euclidean algorithm on two coprime numbers in order to obtain the multiplicative inverse of a modulo b. This is what I get (this is delphi, longword is the basic unsigned 32-bit integer) :

    Code:
    function ModularInverse(a, b: Longword): Longword;
    Var
     X, Y, lastX, lastY, temp, quotient: Longword;
    begin
     X := 0;
     Y := 1;
     Lastx := 1;
     Lasty := 0;
    
     while b <> 0 do
      begin
       Quotient := A div B;
       Temp := B;
       B := A mod B;
       A := Temp;
    
       Temp := X;
       X := Lastx - quotient * X;
       Lastx := temp;
    
       temp := y;
       y := lasty - quotient * y;
       lasty := temp;
      end;
    
     Result := X;
    end;
    But this code keeps failing on even basic inputs like the inverse of 15 modulo 19 ! The answer should be 14 but it just keeps answering either 4, either 10 or sometimes 19 or 0. What the hell is wrong with this ? This is really melting my brain down. I really feel like my computer can't add two numbers right ...

    Thanks all
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Bacterius View Post
    Hey all,
    I've been trying to figure out for a whole evening what was wrong in the code I wrote to apply the extended euclidean algorithm on two coprime numbers in order to obtain the multiplicative inverse of a modulo b. This is what I get (this is delphi, longword is the basic unsigned 32-bit integer) :

    Code:
    function ModularInverse(a, b: Longword): Longword;
    Var
     X, Y, lastX, lastY, temp, quotient: Longword;
    begin
     X := 0;
     Y := 1;
     Lastx := 1;
     Lasty := 0;
    
     while b <> 0 do
      begin
       Quotient := A div B;
       Temp := B;
       B := A mod B;
       A := Temp;
    
       Temp := X;
       X := Lastx - quotient * X;
       Lastx := temp;
    
       temp := y;
       y := lasty - quotient * y;
       lasty := temp;
      end;
    
     Result := X;
    end;
    But this code keeps failing on even basic inputs like the inverse of 15 modulo 19 ! The answer should be 14 but it just keeps answering either 4, either 10 or sometimes 19 or 0. What the hell is wrong with this ? This is really melting my brain down. I really feel like my computer can't add two numbers right ...

    Thanks all
    Case sensitive?

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Super Member Bacterius's Avatar
    Joined
    Nov 2009
    From
    Wellington
    Posts
    927
    Quote Originally Posted by CaptainBlack View Post
    Case sensitive?

    CB
    Hi Captain Black,
    Delphi is not case-sensitive :/
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. [SOLVED] am I wrong here or is my textbook wrong??
    Posted in the Algebra Forum
    Replies: 5
    Last Post: September 25th 2011, 06:13 PM
  2. Replies: 6
    Last Post: August 12th 2009, 03:15 AM
  3. wrong code at matlab - meshgrid and 3d plot
    Posted in the Math Software Forum
    Replies: 2
    Last Post: June 30th 2009, 01:17 AM
  4. Something is wrong with my code
    Posted in the Math Software Forum
    Replies: 1
    Last Post: June 27th 2009, 10:37 PM
  5. What is wrong with this code?
    Posted in the LaTeX Help Forum
    Replies: 4
    Last Post: March 7th 2008, 03:54 AM

Search Tags


/mathhelpforum @mathhelpforum