473,698 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

java.math.BigIn teger

281 Contributor
I don't understand about this program is doing. Could someone please tell me what it is about. Because I have gcd function in one program that calls this function. Thanks a lot
Expand|Select|Wrap|Line Numbers
  1.  
  2. switch (opcode) {
  3. case GCD:       out = b1.gcd(b2).toString();
  4.                             break;
Expand|Select|Wrap|Line Numbers
  1. /**
  2.      * Returns a BigInteger whose value is the greatest common divisor of
  3.      * <tt>abs(this)</tt> and <tt>abs(val)</tt>.  Returns 0 if
  4.      * <tt>this==0 &amp;&amp; val==0</tt>.
  5.      *
  6.      * @param  val value with with the GCD is to be computed.
  7.      * @return <tt>GCD(abs(this), abs(val))</tt>
  8.      */
  9.     public BigInteger gcd(BigInteger val) {
  10.         if (val.signum == 0)
  11.         return this.abs();
  12.     else if (this.signum == 0)
  13.         return val.abs();
  14.  
  15.         MutableBigInteger a = new MutableBigInteger(this);
  16.         MutableBigInteger b = new MutableBigInteger(val);
  17.  
  18.         MutableBigInteger result = a.hybridGCD(b);
  19.  
  20.         return new BigInteger(result, 1);
  21.     }
Mar 19 '07
19 6456
shana07
281 Contributor
Did you set up the directory structure correctly?
I think yes, because I just paste the two classes in that directory.
So in the src\jonelo\suga r\math directory contains classes:

GeneralMath
MutableBigInteg erMutant
BigIntMutant

What else should be done you think...
Mar 20 '07 #11
r035198x
13,262 MVP
I think yes, because I just paste the two classes in that directory.
So in the src\jonelo\suga r\math directory contains classes:

GeneralMath
MutableBigInteg erMutant
BigIntMutant

What else should be done you think...
and you put the package name correctly in the classes in the package statement.
Mar 20 '07 #12
shana07
281 Contributor
and you put the package name correctly in the classes in the package statement.
Hurm, I need to consult you about this naming directory/package which I am not clear about it. I have located my program such this:

D:\Assignments\ GOOD\BigAl\BigA l\bigal-src\
inside there contains:
jonelo\bigal\ * 3 classes: Main, Algorithm, BigAlException
jonelo\sugar\ma th\ * classes: GeneralMath, BigIntMutant, MutableBigInteg erMutant
jonelo\sugar\ut il\ *1 class: General

So, from where should I put import statement in Main class?
This is my import statements in Main class, please correct if I made wrong

package jonelo.bigal;

import java.io.*;
import java.math.BigIn teger;
import java.math.BigDe cimal;

import jonelo.sugar.ma th.GeneralMath;
import jonelo.sugar.ma th.BigIntMutant ;
import jonelo.sugar.ma th.MutableBigIn tegerMutant;
Mar 20 '07 #13
r035198x
13,262 MVP
Hurm, I need to consult you about this naming directory/package which I am not clear about it. I have located my program such this:

D:\Assignments\ GOOD\BigAl\BigA l\bigal-src\
inside there contains:
jonelo\bigal\ * 3 classes: Main, Algorithm, BigAlException
jonelo\sugar\ma th\ * classes: GeneralMath, BigIntMutant, MutableBigInteg erMutant
jonelo\sugar\ut il\ *1 class: General

So, from where should I put import statement in Main class?
This is my import statements in Main class, please correct if I made wrong

package jonelo.bigal;

import java.io.*;
import java.math.BigIn teger;
import java.math.BigDe cimal;

import jonelo.sugar.ma th.GeneralMath;
import jonelo.sugar.ma th.BigIntMutant ;
import jonelo.sugar.ma th.MutableBigIn tegerMutant;
Why don't you try to put all your classes in one package first.
Mar 20 '07 #14
shana07
281 Contributor
Why don't you try to put all your classes in one package first.
This porblem is solved. I created one package like javah/math/BigInteger.
Then I used netbeans to build and run.
About this, i need to ask you: build in netbeans is same as compile?
Then I can see there is one jar created with same program name and build folder?....What happened was, I used prompt before to compile them one by one.

One more thing, what is this statement means?
Expand|Select|Wrap|Line Numbers
  1.  int k = (s1 < s2) ? s1 : s2;
Mar 21 '07 #15
r035198x
13,262 MVP
This porblem is solved. I created one package like javah/math/BigInteger.
Then I used netbeans to build and run.
About this, i need to ask you: build in netbeans is same as compile?
Then I can see there is one jar created with same program name and build folder?....What happened was, I used prompt before to compile them one by one.

One more thing, what is this statement means?
Expand|Select|Wrap|Line Numbers
  1.  int k = (s1 < s2) ? s1 : s2;
It's a short cut way of writing the if-else construct. Play around with it and you will understand it better then.
Mar 21 '07 #16
shana07
281 Contributor
Again about java/math package ...
1. I do need help to understand what is this program for.
2. In what situation then it calls binaryGCD (different binaryGCD & hybridGCD)
please..thanks
Expand|Select|Wrap|Line Numbers
  1.  /**
  2.      * Calculate GCD of this and b. This and b are changed by the computation.
  3.      */
  4.     MutableBigInteger hybridGCD(MutableBigInteger b)      {
  5.         // Use Euclid's algorithm until the numbers are approximately the
  6.         // same length, then use the binary GCD algorithm to find the GCD.
  7.         MutableBigInteger a = this;
  8.         MutableBigInteger q = new MutableBigInteger(),
  9.                           r = new MutableBigInteger();
  10.  
  11.         while (b.intLen != 0) 
  12.         {
  13.             if (Math.abs(a.intLen - b.intLen) < 2)
  14.                 return a.binaryGCD(b);
  15.             a.divide(b, q, r);
  16.             MutableBigInteger swapper = a;
  17.             a = b; b = r; r = swapper;
  18.         }
  19.         return a;
  20.     }
Mar 22 '07 #17
r035198x
13,262 MVP
Again about java/math package ...
1. I do need help to understand what is this program for.
2. In what situation then it calls binaryGCD (different binaryGCD & hybridGCD)
please..thanks
Expand|Select|Wrap|Line Numbers
  1.  /**
  2. * Calculate GCD of this and b. This and b are changed by the computation.
  3. */
  4. MutableBigInteger hybridGCD(MutableBigInteger b) {
  5. // Use Euclid's algorithm until the numbers are approximately the
  6. // same length, then use the binary GCD algorithm to find the GCD.
  7. MutableBigInteger a = this;
  8. MutableBigInteger q = new MutableBigInteger(),
  9. r = new MutableBigInteger();
  10.  
  11. while (b.intLen != 0) 
  12. {
  13. if (Math.abs(a.intLen - b.intLen) < 2)
  14. return a.binaryGCD(b);
  15. a.divide(b, q, r);
  16. MutableBigInteger swapper = a;
  17. a = b; b = r; r = swapper;
  18. }
  19. return a;
  20. }

What it's used for can be anything where one fells they need the gcd and when it calls which method is actually explained by the comment there :

// Use Euclid's algorithm until the numbers are approximately the
// same length, then use the binary GCD algorithm to find the GCD.
Mar 22 '07 #18
shana07
281 Contributor
What it's used for can be anything where one fells they need the gcd and when it calls which method is actually explained by the comment there :

// Use Euclid's algorithm until the numbers are approximately the
// same length, then use the binary GCD algorithm to find the GCD.
meaning to say, if I need to write one calculation program to test :
45.gcd(5) or gcd (45, 5) >>> it will call hybrid function and binary both?
Sorry, I need to paste long codes here to show my query .....thanks
Expand|Select|Wrap|Line Numbers
  1. while (b.intLen != 0) 
  2.         {
  3.             if (Math.abs(a.intLen - b.intLen) < 2)
  4.                 return a.binaryGCD(b);
  5.             a.divide(b, q, r);
  6.             MutableBigInteger swapper = a;
  7.             a = b; b = r; r = swapper;
  8.         }
  9.         return a;
  10.     }
  11.  
  12.  
  13.     /**
  14.      * Calculate GCD of this and v.
  15.      * Assumes that this and v are not zero.
  16.      */
  17.     private MutableBigInteger binaryGCD(MutableBigInteger v)      {
  18.         // Algorithm B from Knuth section 4.5.2
  19.         MutableBigInteger u = this;
  20.         MutableBigInteger q = new MutableBigInteger(),
  21.             r = new MutableBigInteger();
  22.  
  23.         // step B1
  24.         int s1 = u.getLowestSetBit();
  25.         int s2 = v.getLowestSetBit();
  26.         int k = (s1 < s2) ? s1 : s2;  //
  27.         if (k != 0) {
  28.             u.rightShift(k);
  29.             v.rightShift(k);
  30.         }
  31.  
  32.         // step B2
  33.         boolean uOdd = (k==s1);
  34.         MutableBigInteger t = uOdd ? v: u;
  35.         int tsign = uOdd ? -1 : 1;
  36.  
  37.         int lb;
  38.         while ((lb = t.getLowestSetBit()) >= 0) {
  39.             // steps B3 and B4
  40.             t.rightShift(lb);
  41.             // step B5
  42.             if (tsign > 0)
  43.                 u = t;
  44.             else
  45.                 v = t;
  46.  
  47.             // Special case one word numbers
  48.             if (u.intLen < 2 && v.intLen < 2) {
  49.                 int x = u.value[u.offset];
  50.                 int y = v.value[v.offset];
  51.                 x  = binaryGcd(x, y);
  52.                 r.value[0] = x;
  53.                 r.intLen = 1;
  54.                 r.offset = 0;
  55.                 if (k > 0)
  56.                     r.leftShift(k);
  57.                 return r;
  58.             }
  59.  
  60.             // step B6
  61.             if ((tsign = u.difference(v)) == 0)
  62.                 break;
  63.             t = (tsign >= 0) ? u : v;
  64.         }
  65.  
  66.         if (k > 0)
  67.             u.leftShift(k);
  68.         return u;
  69.     }
  70.  
  71.     /**
  72.      * Calculate GCD of a and b interpreted as unsigned integers.
  73.      */
  74.     static int binaryGcd(int a, int b) {
  75.         if (b==0)
  76.             return a;
  77.         if (a==0)
  78.             return b;
  79.  
  80.         int x;
  81.         int aZeros = 0;
  82.         while ((x = (int)a & 0xff) == 0) {
  83.             a >>>= 8;
  84.             aZeros += 8;
  85.         }
  86.         int y = BigInteger.trailingZeroTable[x];
  87.         aZeros += y;
  88.         a >>>= y;
  89.  
  90.         int bZeros = 0;
  91.         while ((x = (int)b & 0xff) == 0) {
  92.             b >>>= 8;
  93.             bZeros += 8;
  94.         }
  95.         y = BigInteger.trailingZeroTable[x];
  96.         bZeros += y;
  97.         b >>>= y;
  98.  
  99.         int t = (aZeros < bZeros ? aZeros : bZeros);
  100.  
  101.         while (a != b) {
  102.             if ((a+0x80000000) > (b+0x80000000)) {  // a > b as unsigned
  103.                 a -= b;
  104.  
  105.                 while ((x = (int)a & 0xff) == 0)
  106.                     a >>>= 8;
  107.                 a >>>= BigInteger.trailingZeroTable[x];
  108.             } else {
  109.                 b -= a;
  110.  
  111.                 while ((x = (int)b & 0xff) == 0)
  112.                     b >>>= 8;
  113.                 b >>>= BigInteger.trailingZeroTable[x];
  114.             }
  115.         }
  116.         return a<<t;
  117.     }
Mar 22 '07 #19
shana07
281 Contributor
Need help, If someone has experience in core java class - MutableBigInteg er, please share some with me. BinaryGCD here is what for...Let say I have gcd(45, 5) is that involves with binaryGCD(Mutab leBigInteger v)) too?
Thankss
Mar 22 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3314
by: Cheetah | last post by:
Does anyone know of some public/open source implementations of Trig funtions - ie sin, cos, tan - for Java that operate with BigIntegers or BigDecimals. Even better would be a library which can add vectors. I'm starting with amplitudes and degrees, and need to add the vectors to come out with a final vector (amplitude). This kind of thing would be used in Quantum Mechanics/QED for example perhaps to calculate probabilities. I'm using it...
73
8017
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities as a standard portable part of the core language, the LISP package, and the java.lang package, respectively. Both have big integers, although only LISP has rationals as far as I can tell. Because CL supports keyword arguments, it has a wider range...
458
21202
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
2
7878
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the path, import java.math.*;//*loan calculator import java.text.*;//*formats numbers public class 3 Mortgage loans { // declare class variable array int mortgage calculator
1
3293
by: feathers75 | last post by:
-------------------------------------------------------------------------------- First, Hello eveyone and I am new to Java. I am trying to create a Java program that will calculate a person BMI based on user input of weight (in pounds) and heigh (in inches) and then display what the BMI is and give the a message that tells them where they fit plus I want to then be able to tell them how much weight they need to loss (or gain if...
5
6282
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is JDK 6 downloadable from http://java.sun.com/javase/downloads/index.jsp. I will be using JDK 5(update 8)
6
13564
by: mearvk | last post by:
Does C++ or C have something roughly equivalent to this: http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html What I need is a way to quickly convert between decimal and binary and from char*/string to a numeric representation. Thanks!
318
10996
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... http://kingrazi.blogspot.com/2008/05/shootout-c-net-vs-java-benchmarks.html Just to keep the post on topic for my friends at comp.lang.c++, how do I play default windows sounds with C++?
11
10505
by: imranisb | last post by:
Hi, Im developing RSA cryptography based application in VC#.Net 2.0 and need to use "BigInteger.probablePrime()". I know that VC#.Net 2.0 doesn't have the BigInteger class but found that one buddy created BigInteger class but it is missing some overloading function like BigInteger.probablePrime(). The code of the BigInteger class developed in the C#.Net can be found here: http://www.codeproject.com/KB/cs/biginteger.aspx
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.