sandeshMC

Extended Euclidean Theorem

Apr 6th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ExtendedEuclideanTheorem {
  4.  
  5.     static int[] gcd(int p, int q) {
  6.         if (q == 0)
  7.             return new int[] { p, 1, 0 };
  8.  
  9.         int[] vals = gcd(q, p % q);
  10.         int d = vals[0];
  11.         int a = vals[2];
  12.         int b = vals[1] - (p / q) * vals[2];
  13.         return new int[] { d, a, b };
  14.     }
  15.  
  16.     public static void main(String[] args) {
  17.         Scanner sc = new Scanner(System.in);
  18.         System.out.println("Enter the p and q");
  19.         int p = sc.nextInt();
  20.         int q = sc.nextInt();
  21.         int vals[] = gcd(p, q);
  22.         System.out.println("gcd(" + p + ", " + q + ") = " + vals[0]);
  23.         System.out.println(vals[1] + "(" + p + ") + " + vals[2] + "(" + q
  24.                 + ") = " + vals[0]);
  25.     }
  26. }
  27.  
  28. /*
  29. OUTPUT:
  30. Enter the p and q
  31. 65537
  32. 3511
  33. gcd(65537, 3511) = 1
  34. -1405(65537) + 26226(3511) = 1
  35. */
Advertisement
Add Comment
Please, Sign In to add comment