Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package javaapplication7;
  7. import java.lang.Math;
  8. import java.math.BigInteger;
  9.  
  10. /**
  11. *
  12. * @author student
  13. */
  14. public class JavaApplication7 {
  15.  
  16.  
  17. //recursively searches for the sqr root of a in interval [left, right]
  18. private static BigInteger NaiveSquareRootSearch(BigInteger a, BigInteger left,
  19. BigInteger right)
  20. {
  21. // fix root as the arithmetic mean of left and right
  22. BigInteger root = left.add(right).shiftRight(1);
  23. // if the root is not between [root, root+1],
  24. //is not an integer and root is our best integer approximation
  25. if(!((root.pow(2).compareTo(a) == -
  26. 1)&&(root.add(BigInteger.ONE).pow(2).compareTo(a) == 1))){
  27. if (root.pow(2).compareTo(a) == -1) root = NaiveSquareRootSearch(a, root,
  28. right);
  29. if (root.pow(2).compareTo(a) == 1) root = NaiveSquareRootSearch(a, left,
  30. root);
  31. }
  32. return root;
  33. }
  34.  
  35. public static BigInteger SquareRoot(BigInteger a)
  36. {
  37. return NaiveSquareRootSearch(a, BigInteger.ZERO, a);
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44. /**
  45. * @param args the command line arguments
  46. */
  47. public static void main(String[] args) {
  48.  
  49. BigInteger four = new BigInteger("4");
  50. BigInteger two = new BigInteger("2");
  51.  
  52. BigInteger n = new BigInteger("837210799");
  53. BigInteger e = new BigInteger("7");
  54. BigInteger d = new BigInteger("478341751");
  55. BigInteger e2 = new BigInteger("17");
  56.  
  57. BigInteger de = d.multiply(e);
  58. BigInteger k = (de.subtract(BigInteger.ONE)).divide(n);
  59.  
  60. BigInteger mymod = (de.subtract(BigInteger.ONE)).mod(n);
  61. if(mymod.compareTo(BigInteger.ZERO) != 0)
  62. k = k.add(BigInteger.ONE);
  63.  
  64. BigInteger S = (((k.multiply(n.add(BigInteger.ONE))).add(BigInteger.ONE)).subtract(de)).divide(k);
  65. BigInteger delta = (S.pow(2)).subtract(n.multiply(four));
  66. BigInteger x1 = (S.add(SquareRoot(delta))).divide(two);
  67. BigInteger x2 = (S.subtract(SquareRoot(delta))).divide(two);
  68.  
  69. System.out.println("S\n" + S+ "\ndelta\n"+delta +"\nx1\n" + x1 +"\nx2\n" + x2);
  70.  
  71. BigInteger d2=(e2.modInverse(BigInteger.ONE)).mod(n);
  72. System.out.println("sal " + SquareRoot(new BigInteger("100")));
  73. System.out.println(d2);
  74.  
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement