关于java编程中的有理数,计算(1/2+;3/4+;…+;99/100)^2

关于java编程中的有理数,计算(1/2+;3/4+;…+;99/100)^2,java,biginteger,rational-number,Java,Biginteger,Rational Number,此代码的目的是计算(1/2+3/4+…+99/100)^2。但是我的循环无法正确执行。 r1的结果是3/4而不是99/100,我的代码怎么了? 我认为我的循环可以运行,因为y我可以正确地得到它。 那么,我如何更正代码并使其能够计算(1/2+3/4+…+99/100)^2?谢谢你的回答 import java.math.BigInteger; public class Rational { // Data fields for numerator and denominator private

此代码的目的是计算(1/2+3/4+…+99/100)^2。但是我的循环无法正确执行。 r1的结果是3/4而不是99/100,我的代码怎么了? 我认为我的循环可以运行,因为y我可以正确地得到它。 那么,我如何更正代码并使其能够计算(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;
 }
}
import java.math.biginger;
公共阶级理性{
//分子和分母的数据字段
私有BigInteger分子=BigInteger.0;
私有BigInteger分母=BigInteger.ONE;
/**构造具有默认属性的rational*/
公共理性{
这个(biginger.ZERO,biginger.ONE);
}
/**用指定的分子和分母构造有理数*/
公共有理数(大整数分子,大整数分母){
BigInteger gcd=新的BigInteger(String.valueOf)gcd(分子,
分母);
BigInteger r1=新的
BigInteger(String.valueOf(分母.compareTo(BigInteger.ZERO));
这个。分子=(r1。乘(分子))。除(gcd);
this.densor=(densor.abs()).divide(gcd);
}
/**查找两个数字的GCD*/
专用静态长gcd(BigInteger n,BigInteger d){
BigInteger n1=n.abs();
大整数n2=d.abs();
int-gcd=1;

对于(int k=1;(new biginger(String.valueOf(k))).compareTo(n1)不清楚您的数字序列是什么,但我将采用以下假设:

如果您的目标是简单地返回
(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+5/6+…+97/98+99/100)的值^2
公共整数计算(){
双分母=2;
双分子=分母-1;//在序列中,分子总是比分母小1
双和=0;

虽然(分母不清楚您的数字序列是什么,但我将采用以下假设:

如果您的目标是简单地返回
(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+5/6+…+97/98+99/100)的值^2
公共整数计算(){
双分母=2;
双分子=分母-1;//在序列中,分子总是比分母小1
双和=0;

虽然(分母让我们写得不那么混乱,没有所有不必要的东西和混乱的循环。总和的定义是(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));
}
我无法验证,但它看起来足够合理。预期的答案是“略小于502”(因为50项的平方接近1,如果可以称为0.5),这已经足够接近了


顺便说一句,停止在
Rational
中的任何地方使用
String.valueOf
。只需使用数字即可。
biginger
已经实现了
gcd
,您不必编写自己的(效率较低)版本。我必须替换它,否则它花费的时间太长。

让我们写得不那么混乱,没有所有不必要的东西和混乱的循环。总和的定义是(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));
}
我无法验证,但它看起来足够合理。预期的答案是“略小于502”(因为50项的平方接近1,如果可以称为0.5),这已经足够接近了


顺便说一句,停止在
Rational
中的任何地方使用
String.valueOf
。只需使用数字即可。
biginger
已经实现了
gcd
,您不必编写自己的(效率较低)版本。我必须替换它,否则它花费的时间太长。

循环唯一改变的事情是
sum
b
y
(这很明显,因为它确实是循环说要做的事情)。没有理由会影响
r1
s1
s2
r0
。那么,我如何才能让它们进入循环而不出错呢?我曾尝试在循环内生成r0。但整个程序失败了。循环唯一改变的事情是
sum
b
y
(这是显而易见的,因为这是循环的字面意思)。没有理由会影响
r1
s1
s2
r0
。因此,如何将它们放入循环中而不出错呢?我曾尝试在循环中生成r0。但整个程序失败了。不过,简单地将这些整数除以是没有用的,尤其是因为分母总是更大比分子大。你说得对,哈罗德,我把变量编辑成了双倍。我猜结果也是这样吗?然后我们得到了大致相同的结果,这表明简单地除以这些整数是没有用的,特别是因为分母总是比分子大。你说得对,哈罗德,我把变量编辑成了双倍.结果也是我猜的?然后我们得到了大致相同的结果,这对你的答案是肯定的!但是如果我不添加String.valueOf,eclipse会告诉我类似于“构造函数biginger(long)”的东西是不可见的。我该怎么处理呢?谢谢!@Ben.W如果你真的需要从长整数中构造一个大整数,有
biginger.valueOf
,但你没有(至少没有)。
分子.gcd(分母)
已经是一个
BigInteger
。谢谢,我已经更正了我的代码。事实上,这是我的作业,我必须将原来的int-Rational类更改为BigInteger-Rational类。所以我刚刚更改了:)。我更正了我的Rational类,所以gcd将不被使用。有趣的是,如果我不将代码更改为
BigInteger gcd=分子.gcd(分母)87593039510089573189394173247956745677798336081
-----------------------------------------------
   38416307357189261992010230523038591203840000