关于java编程中的有理数,计算(1/2+3/4+...+99/100)^2
About rational number in java programming, to calculate (1/2+3/4+...+99/100)^2
这段代码的目的是计算(1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。
r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?
我认为我的循环可以是 运行 因为我可以正确地得到它。
那么我该如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 呢?谢谢你的回答。
import java.math.BigInteger;
public class Rational {
// Data fields for numerator and denominator
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
/** Construct a rational with default properties */
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
/** Construct a rational with specified numerator and denominator */
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd=new BigInteger(String.valueOf(gcd(numerator,
denominator)));
BigInteger r1=new
BigInteger(String.valueOf(denominator.compareTo(BigInteger.ZERO)));
this.numerator = (r1.multiply(numerator)).divide(gcd);
this.denominator = (denominator.abs()).divide(gcd);
}
/** Find GCD of two numbers */
private static long gcd(BigInteger n, BigInteger d) {
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
int gcd = 1;
for (int k = 1; (new BigInteger(String.valueOf(k))).compareTo(n1)<=0 &&
(new BigInteger(String.valueOf(k))).compareTo(n2)<=0; k++) {
if (n1.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO) &&
n2.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO))
gcd = k;
}
return gcd;
}
/** Return numerator */
public BigInteger getNumerator() {
return numerator;
}
/** Return denominator */
public BigInteger getDenominator() {
return denominator;
}
/** Add a rational number to this rational */
public Rational add(Rational secondRational) {
BigInteger n =
numerator.multiply(secondRational.getDenominator())
.add(denominator.multiply(sec
ondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Subtract a rational number from this rational */
public Rational subtract(Rational secondRational) {
BigInteger n =
(numerator.multiply(secondRational.getDenominator()))
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Multiply a rational number to this rational */
public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Divide a rational number from this rational */
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
return new Rational(n, d);
}
/** Compute the square of this rational number*/
public Rational square() {
BigInteger n = numerator.multiply(numerator);
BigInteger d = denominator.multiply(denominator);
return new Rational(n, d);
}
/** toString */
public String toString() {
return numerator + "/" + denominator;
}
}
这是 testRational class
导入 java.math.BigInteger;
public class TestRational {
public static void main(String[]args){
int y = 1;
BigInteger i=new BigInteger(String.valueOf(1));
BigInteger a=new BigInteger(String.valueOf(2));
BigInteger b=new BigInteger(String.valueOf(3));
BigInteger c=new BigInteger(String.valueOf(5));
Rational sum = new Rational(BigInteger.ZERO,a);
Rational r0 = new Rational(b,b.add(i));
Rational r2 = new Rational(a,c);
Rational r3 = new Rational(a,c);
Rational s1 = r3.multiply(r2);
Rational s2 = r3.square();
Rational s3 = r2.divide(r3);
Rational r1 = new Rational(i,a);
do{
sum = sum.add(r0);
b = b.add(a);
y++;
}while(y<49);
System.out.println(sum.multiply(sum));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(r0);
}
}
这段代码的目的是计算(1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。
r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?
我认为我的循环可以是 运行 因为我可以正确地得到它。
那么我该如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 呢?谢谢你的回答。
这段代码的目的是计算(1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。
r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?
我认为我的循环可以是 运行 因为我可以正确地得到它。
那么我该如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 呢?谢谢你的回答。
不清楚你的数字序列是什么,但我会采用以下假设:
如果您的目标只是 return (1/2 + 3/4 + 5/6 + ... + 97/98 + 99/100) ^ 2
的价值。那么我建议如下:
//This method will return the value of (1/2 + 3/4 + 5/6 + ... + 97/98 + 99/100) ^ 2
public int calc(){
double denominator = 2;
double numerator = denominator - 1; //in your sequence, numerator is always 1 less than denominator
double sum = 0;
while(denominator <= 100){
sum = sum + (numerator / denominator); //shorthand sum += (numerator / denominator);
denominator = denominator + 2; //shorthand denominator += 2;
numerator = denominator - 1;
}
return sum * sum; //this is equivalent to sum ^ 2
}
让我们把它写得不那么混乱,没有所有不必要的东西和令人困惑的循环。总和的定义是 (1/2 + 3/4 ... 99/100) 所以让我们从创建总和中的所有分数开始:
for (int i = 1; i <= 99; i += 2) {
BigRational t = new BigRational(BigInteger.valueOf(i), BigInteger.valueOf(i + 1));
}
它们必须相加,所以必须在循环外声明一个变量来将所有这些分数相加为:
Rational sum = new Rational();
for (int i = 1; i <= 99; i += 2) {
Rational t = new Rational(BigInteger.valueOf(i), BigInteger.valueOf(i + 1));
sum = sum.add(t);
}
然后平方,你就有了答案。我得到:
87593039510089573189394173247956745677798336081
-----------------------------------------------
38416307357189261992010230523038591203840000
我无法验证,但看起来很合理。预期的答案是 "a bit less than 502"(因为它是接近 1 的 50 项的平方,如果可以称其为 0.5),这已经足够接近了。
顺便说一下,在Rational
中不要到处使用String.valueOf
。只处理数字。 BigInteger
已经实现了 gcd
,您不必编写自己的(效率较低的)版本。我必须更换它,否则会花费太长时间。
这段代码的目的是计算(1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。 r1 的结果是 3/4 而不是 99/100,我的代码有什么问题? 我认为我的循环可以是 运行 因为我可以正确地得到它。 那么我该如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 呢?谢谢你的回答。
import java.math.BigInteger;
public class Rational {
// Data fields for numerator and denominator
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
/** Construct a rational with default properties */
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
/** Construct a rational with specified numerator and denominator */
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd=new BigInteger(String.valueOf(gcd(numerator,
denominator)));
BigInteger r1=new
BigInteger(String.valueOf(denominator.compareTo(BigInteger.ZERO)));
this.numerator = (r1.multiply(numerator)).divide(gcd);
this.denominator = (denominator.abs()).divide(gcd);
}
/** Find GCD of two numbers */
private static long gcd(BigInteger n, BigInteger d) {
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
int gcd = 1;
for (int k = 1; (new BigInteger(String.valueOf(k))).compareTo(n1)<=0 &&
(new BigInteger(String.valueOf(k))).compareTo(n2)<=0; k++) {
if (n1.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO) &&
n2.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO))
gcd = k;
}
return gcd;
}
/** Return numerator */
public BigInteger getNumerator() {
return numerator;
}
/** Return denominator */
public BigInteger getDenominator() {
return denominator;
}
/** Add a rational number to this rational */
public Rational add(Rational secondRational) {
BigInteger n =
numerator.multiply(secondRational.getDenominator())
.add(denominator.multiply(sec
ondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Subtract a rational number from this rational */
public Rational subtract(Rational secondRational) {
BigInteger n =
(numerator.multiply(secondRational.getDenominator()))
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Multiply a rational number to this rational */
public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Divide a rational number from this rational */
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
return new Rational(n, d);
}
/** Compute the square of this rational number*/
public Rational square() {
BigInteger n = numerator.multiply(numerator);
BigInteger d = denominator.multiply(denominator);
return new Rational(n, d);
}
/** toString */
public String toString() {
return numerator + "/" + denominator;
}
}
这是 testRational class 导入 java.math.BigInteger;
public class TestRational {
public static void main(String[]args){
int y = 1;
BigInteger i=new BigInteger(String.valueOf(1));
BigInteger a=new BigInteger(String.valueOf(2));
BigInteger b=new BigInteger(String.valueOf(3));
BigInteger c=new BigInteger(String.valueOf(5));
Rational sum = new Rational(BigInteger.ZERO,a);
Rational r0 = new Rational(b,b.add(i));
Rational r2 = new Rational(a,c);
Rational r3 = new Rational(a,c);
Rational s1 = r3.multiply(r2);
Rational s2 = r3.square();
Rational s3 = r2.divide(r3);
Rational r1 = new Rational(i,a);
do{
sum = sum.add(r0);
b = b.add(a);
y++;
}while(y<49);
System.out.println(sum.multiply(sum));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(r0);
}
}
这段代码的目的是计算(1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。 r1 的结果是 3/4 而不是 99/100,我的代码有什么问题? 我认为我的循环可以是 运行 因为我可以正确地得到它。 那么我该如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 呢?谢谢你的回答。 这段代码的目的是计算(1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。 r1 的结果是 3/4 而不是 99/100,我的代码有什么问题? 我认为我的循环可以是 运行 因为我可以正确地得到它。 那么我该如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 呢?谢谢你的回答。
不清楚你的数字序列是什么,但我会采用以下假设:
如果您的目标只是 return (1/2 + 3/4 + 5/6 + ... + 97/98 + 99/100) ^ 2
的价值。那么我建议如下:
//This method will return the value of (1/2 + 3/4 + 5/6 + ... + 97/98 + 99/100) ^ 2
public int calc(){
double denominator = 2;
double numerator = denominator - 1; //in your sequence, numerator is always 1 less than denominator
double sum = 0;
while(denominator <= 100){
sum = sum + (numerator / denominator); //shorthand sum += (numerator / denominator);
denominator = denominator + 2; //shorthand denominator += 2;
numerator = denominator - 1;
}
return sum * sum; //this is equivalent to sum ^ 2
}
让我们把它写得不那么混乱,没有所有不必要的东西和令人困惑的循环。总和的定义是 (1/2 + 3/4 ... 99/100) 所以让我们从创建总和中的所有分数开始:
for (int i = 1; i <= 99; i += 2) {
BigRational t = new BigRational(BigInteger.valueOf(i), BigInteger.valueOf(i + 1));
}
它们必须相加,所以必须在循环外声明一个变量来将所有这些分数相加为:
Rational sum = new Rational();
for (int i = 1; i <= 99; i += 2) {
Rational t = new Rational(BigInteger.valueOf(i), BigInteger.valueOf(i + 1));
sum = sum.add(t);
}
然后平方,你就有了答案。我得到:
87593039510089573189394173247956745677798336081
-----------------------------------------------
38416307357189261992010230523038591203840000
我无法验证,但看起来很合理。预期的答案是 "a bit less than 502"(因为它是接近 1 的 50 项的平方,如果可以称其为 0.5),这已经足够接近了。
顺便说一下,在Rational
中不要到处使用String.valueOf
。只处理数字。 BigInteger
已经实现了 gcd
,您不必编写自己的(效率较低的)版本。我必须更换它,否则会花费太长时间。