SHOW:
|
|
- or go back to the newest paste.
1 | package com.ignatieff.zeta; | |
2 | ||
3 | import java.math.BigDecimal; | |
4 | import java.math.MathContext; | |
5 | import java.math.RoundingMode; | |
6 | import java.util.Scanner; | |
7 | ||
8 | public class Main { | |
9 | ||
10 | private static BigDecimal PI; | |
11 | private static BigDecimal PiPow; | |
12 | public static void main(String[] args) { | |
13 | Scanner s = new Scanner(System.in); | |
14 | - | //The hypothesis is that Zeta(3) = pi^3 / n; 5 < n < 90 |
14 | + | |
15 | int x=3; | |
16 | do{ | |
17 | System.out.print("Calculate Zeta(s), where s = "); | |
18 | try{ | |
19 | x=Integer.parseInt(s.nextLine());}catch(Exception e){} | |
20 | }while(x==s.radix()); | |
21 | do{ | |
22 | System.out.print("Precision (digits): "); | |
23 | try{ | |
24 | rounds=Integer.parseInt(s.nextLine());}catch(Exception e){} | |
25 | }while(rounds==s.radix()); | |
26 | ||
27 | MathContext mc = new MathContext(rounds+20, RoundingMode.HALF_UP); | |
28 | System.out.println("Calculating PI to " + rounds + " decimal places..."); | |
29 | PI = Pi.pi(rounds); | |
30 | ||
31 | ||
32 | //The digit d affected by zeta iteration i, is: d = log10(i^3) | |
33 | BigDecimal p = new BigDecimal(10).pow(rounds); | |
34 | BigDecimal root = Zeta.cuberoot(p, 100, mc); | |
35 | long r = root.round(new MathContext(1)).longValue(); | |
36 | System.out.println("Approximating the riemann zeta function to " + rounds + " decimals ("+r+" rounds).\n"); | |
37 | BigDecimal zeta = Zeta.zeta(x, r, mc); | |
38 | System.out.println("Finding best match..."); | |
39 | BigDecimal smallestDev = new BigDecimal(Integer.MAX_VALUE); | |
40 | BigDecimal val = new BigDecimal(0); | |
41 | int bestMatch = -1; | |
42 | int pi = 1; | |
43 | for(int P=0;P<51;P++){ | |
44 | PiPow = PI.pow(P); | |
45 | for(int i=0;i<5000;i++){ | |
46 | BigDecimal exact = PiPow.divide(new BigDecimal(i+1),mc); | |
47 | BigDecimal testDev = (zeta.subtract(exact,mc)).abs(); | |
48 | if(testDev.compareTo(smallestDev) == -1){ | |
49 | bestMatch=i; | |
50 | smallestDev=testDev; | |
51 | val = exact; | |
52 | pi=P; | |
53 | } | |
54 | }} | |
55 | ||
56 | smallestDev = smallestDev.setScale(rounds, RoundingMode.CEILING); | |
57 | zeta = zeta.setScale(rounds, RoundingMode.CEILING); | |
58 | val = val.setScale(rounds, RoundingMode.CEILING); | |
59 | ||
60 | System.out.println("The best approximation is (Pi^"+pi+")/"+(bestMatch+1)+" - with a deviation of " + smallestDev.toString()); | |
61 | System.out.println(" - Zeta("+x+") = " + zeta.toString()); | |
62 | System.out.println(" - (Pi^"+pi+")/"+(bestMatch+1)+" = " +val); | |
63 | } | |
64 | ||
65 | } |