有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

计算嵌套根常数的递归Java程序

[Image from MathWorld, WolframAlpha

使用下面的代码,我能够估计它高达: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));
}

然而,我无法通过这里的递归得到更准确的答案。从11000增加递归调用的数量会给我一个StackOverflowError。我还查看了如何在这里使用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.

此外,我也研究了Java8流,但这些对我来说没有多大意义(因为我是编程和Java新手),也不确定这是否适用于这里

谢谢你的帮助


共 (3) 个答案

  1. # 1 楼答案

    如果正确地实现算法,您将获得更高的精度

    编辑:您的实现没有错误,但我不会这样做。。。(sum的第5行可以返回x的sqrt,而不是a,也可以是0。)

    其次,以迭代方式完成这一切的关键是向后走

    k = 11000
    ans = 0
    while k > 0:
        ans+=k
        ans**=0.5
        k-=1
    return(ans)
    
  2. # 2 楼答案

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

    如果我们调用迭代极限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.

  3. # 3 楼答案

    comment by Tibrogargan中提供的链接获取answer by Mike 'Pomax' Kamermans中的代码,使用answer by barwnikk中的算法将其转换为使用BigDecimal,您将得到以下代码:

    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个数字似乎无法打印,所以我停在那里。也开始变慢了。;-)