Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Extended Euclid BigInteger version

807603Dec 31 2007 — edited Dec 31 2007
Here's a simple int version
// Extended Euclid - returns array [d, a, b] such that d = gcd(p, q), ap + bq = d
		class Return {
			
			public int d, x, y;
		
		}
	
	   	public Return EEgcd(int p, int q) {
	      if (q == 0) {
	     		Return r = new Return();
	      	r.d = p;
	      	r.x = 1;
	      	r.y = 0;
	         return r;
		  }
		  
	      Return r = EEgcd(q, p % q);
	      										
	      Return r2 = new Return();				
	      r2.d = r.d;
	      						
	      r2.x = r.y;
	      										
	      r2.y = r.x - (p / q) * r.y;
	      										
	      return r2;
	   }
I'm trying to do a BigInteger version but I'm not getting the same results
// Extended Euclid - returns array [d, a, b] such that d = gcd(p, q), ap + bq = d
		class Return {
			
			public BigInteger d, x, y;
		
		}
	
	   	public Return EEgcd(BigInteger p, BigInteger q) {
	      if (q.compareTo(BigInteger.ZERO) == 1) {
	     		Return r = new Return();
	      	r.d = p;
	      	r.x = BigInteger.ONE;
	      	r.y = BigInteger.ZERO;
	         return r;
		  }
		  
	      Return r = EEgcd(q, p.mod(q));
	      										
	      Return r2 = new Return();				
	      r2.d = r.d;
	      						
	      r2.x = r.y;
	      										
	      r2.y = r.x.subtract((p.divide(q)).multiply(r.y));

		return r2;
	   }
example EEgcd(99, 78) should equal 3 which it does for the int version but equals 99 for the BigInt version

Where have I gone wrong?

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 28 2008
Added on Dec 31 2007
2 comments
1,000 views