Java 计算嵌套根常数的程序

Java programme to compute the nested radical constant

使用下面的代码我可以估计它高达:1.7579327566180045

public class Sum {
static int count = 0;
static double a = 0;
public static void main(String[] args) {
    int x = 0;
    System.out.println(sum(x));
}

public static double sum(int x){
    count++;
    x++;
    if (count == 11000){
        return a;
    }
    return a = Math.sqrt(x+sum(x));
}

然而,我无法通过此处的递归得到更准确的答案。从 11,000 增加递归调用的次数给了我 WhosebugError。我还查看了如何在这里使用 java 的 BigDecimal,但我无法在这里实现它,老实说,我不确定这是否有帮助。

Question :- how can I make this programme compute the constant to more decimal places and more accurately? I'm not certain if this I can compute it iteratively or not either. I wasn't able to work out an iterative definition.

此外,我也研究了 Java 8 Streams,但这些对我来说意义不大(因为我是编程新手并且 Java)并且不确定是否适用于此处。

感谢您的帮助!

如果正确实施算法,您将获得更高的准确性。

编辑: 你的实现并没有错,但我不会这样做......(sum 的第 5 行可以 return x 的 sqrt 而不是的,也可能只是 0。)

其次,迭代执行此操作的关键是倒退。

k = 11000
ans = 0
while k > 0:
    ans+=k
    ans**=0.5
    k-=1
return(ans)

如果您唯一的问题是堆栈:请不要使用递归,只需使用普通迭代即可。

如果我们称迭代极限为n,则根常数为:

sqrt(1 + sqrt(2 + sqrt(3 + sqrt(4 + ... sqrt(... + sqrt(n))...))))

等同于:

sqrt(...(sqrt(sqrt(sqrt(n) + (n-1)) + (n-2)) + (n-3)) + ... )

所以让我们使用它:

public class Test {
  public Test(int iterations) {
    int n = iterations;
    double sum = 0;
    while (n > 0) {
      sum = Math.sqrt(sum + n--);
    }
    System.out.printf("Precision using %d iterations: %1.50f\n", iterations, sum);
  }

  public static void main(String[] args) {
    new Test(1);
    new Test(10);
    new Test(100);
    new Test(1000);
    new Test(10000);
    new Test(100000);
    new Test(1000000);
    new Test(10000000);
    new Test(100000000);
  }
}

当然,由于您需要精确度,这不会有太大帮助:现在您不再 运行 陷入堆栈问题,您 运行 陷入这样一个事实IEEE 浮点数不适用于数学上的精确计算:

Precision using 1 iterations: 1.00000000000000000000000000000000000000000000000000
Precision using 10 iterations: 1.75793261139383100000000000000000000000000000000000
Precision using 100 iterations: 1.75793275661800450000000000000000000000000000000000
Precision using 1000 iterations: 1.75793275661800450000000000000000000000000000000000
Precision using 10000 iterations: 1.75793275661800450000000000000000000000000000000000
Precision using 100000 iterations: 1.75793275661800450000000000000000000000000000000000
Precision using 1000000 iterations: 1.75793275661800450000000000000000000000000000000000
Precision using 10000000 iterations: 1.75793275661800450000000000000000000000000000000000
Precision using 100000000 iterations: 1.75793275661800450000000000000000000000000000000000

根据 official documentation:

This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

获取 , converting it to use BigDecimal using the algorithm in answer by barwnikk from the link provided in 中的代码,您将得到以下代码:

private static void nestedRadicalConstant(int iterations, int scale) {
    BigDecimal sum = BigDecimal.ZERO;
    for (int n = iterations; n > 0; n--)
        sum = sqrt(sum.add(BigDecimal.valueOf(n)), scale);
    System.out.printf("Precision using %9d iterations: %s\n", iterations, sum);
}
private static final BigDecimal TWO = BigDecimal.valueOf(2);
private static BigDecimal sqrt(BigDecimal a, int scale) {
    BigDecimal x0 = BigDecimal.ZERO;
    BigDecimal x1 = new BigDecimal(Math.sqrt(a.doubleValue()));
    while (! x0.equals(x1)) {
        x0 = x1;
        x1 = a.divide(x0, scale, BigDecimal.ROUND_HALF_UP);
        x1 = x1.add(x0);
        x1 = x1.divide(TWO, scale, BigDecimal.ROUND_HALF_UP);
    }
    return x1;
}

在 1000 次迭代中,您似乎获得了 1596 位精度。测试:

nestedRadicalConstant(1, 2);
nestedRadicalConstant(10, 20);
nestedRadicalConstant(100, 120);
nestedRadicalConstant(1000, 1620);
nestedRadicalConstant(10000, 2000);

产生这个输出:

Precision using         1 iterations: 1.00
Precision using        10 iterations: 1.75793261139383098942
Precision using       100 iterations: 1.757932756618004532708819638218138527653199922146837704310135500385110232674446757572344554000259452970932471847765477212
Precision using      1000 iterations: 1.757932756618004532708819638218138527653199922146837704310135500385110232674446757572344554000259452970932471847826956725286405867741108546115435116745974827649802384369489120411842037876481995830644570345768467313417541513449577173273720962022100603227554116598015407552297612944579699112707719478877860007819516309923396999343623052775352496605485188121304121230743966852549640366715265942215947576652412589521440394432605735991324822082490634153150397875302128772604959532494672112007991822456833844067286433074237282346571947808094291349553420592279925860366170372859630816687183328634908728532926587173888717587225690606966741535388517308782986073313679762614334220034550147482219697344628499290204994260780123338419145972718423791086759045639529537528043251120937807502935923611917615270426436487465911939829459953781691083134966345861642367678466818801916873226676954205133566864879409563789163447674389255347895570972640620596122532631802815634393718529817582444581463125494708586493852134993196476027405424112251632598737556657076790516333930301963846032409179377260137724948433124123721498603941391880712274921521093576064227183964712879727605419662075877641516168770731031830438884407663198533406472178289280964677785213403598029666777396176022091845158367038947205930644559617550964376557881938238936999861972092712003303733154006164548042213795996830518359866201345560149007762659936776433223239718347842294405131084630617937696469599012405313392949671129259837927464454348595975072906890699729096515457528663221822249478993545431942135457377664898687489112921130467353566525378019109731173223933551193081888
Precision using     10000 iterations: 1.75793275661800453270881963821813852765319992214683770431013550038511023267444675757234455400025945297093247184782695672528640586774110854611543511674597482764980238436948912041184203787648199583064457034576846731341754151344957717327372096202210060322755411659801540755229761294457969911270771947887786000781951630992339699934362305277535249660548518812130412123074396685254964036671526594221594757665241258952144039443260573599132482208249063415315039787530212877260495953249467211200799182245683384406728643307423728234657194780809429134955342059227992586036617037285963081668718332863490872853292658717388871758722569060696674153538851730878298607331367976261433422003455014748221969734462849929020499426078012333841914597271842379108675904563952953752804325112093780750293592361191761527042643648746591193982945995378169108313496634586164236767846681880191687322667695420513356686487940956378916344767438925534789557097264062059612253263180281563439371852981758244458146312549470858649385213499319647602740542411225163259873755665707679051633393030196384603240917937726013772494843312412372149860394139188071227492152109357606422718396471287972760541966207587764151616877073103183043888440766319853340647217828928096467778521340359802966677739617602209184515836703894720593064455961755096437655788193823893699986197209271200330373315400616454804221379599683051835986620134556014900776265993677643322323971834784229440513108463061793769646959901240531339294967112925983792746445434859597507290689069972909651545752866322182224947899354543194213545737766489868748911292113046735356652537801910986407230565075451949088994252961807114520240492889839699378820767287005364075922299290226616859542304563610837999855263494755487315890823802348384905110801341645915817905877028789375061746193512837109024779585044655168397357131113466177623319647140519047121827755000973602827506971619064114104567151734665774836770974378326977363987102385865012823517670987447776358377974581150446359028968379541355377763

4600多位好像打印不出来,就到此为止了。也开始变慢了。 ;-)