Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ignatieff.zeta;
- import java.math.BigDecimal;
- import java.math.MathContext;
- import java.math.RoundingMode;
- import java.util.Scanner;
- public class Main {
- private static BigDecimal PI;
- private static BigDecimal PiPow;
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int rounds = s.radix();
- int x=3;
- do{
- System.out.print("Calculate Zeta(s), where s = ");
- try{
- x=Integer.parseInt(s.nextLine());}catch(Exception e){}
- }while(x==s.radix());
- do{
- System.out.print("Precision (digits): ");
- try{
- rounds=Integer.parseInt(s.nextLine());}catch(Exception e){}
- }while(rounds==s.radix());
- MathContext mc = new MathContext(rounds+20, RoundingMode.HALF_UP);
- System.out.println("Calculating PI to " + rounds + " decimal places...");
- PI = Pi.pi(rounds);
- //The digit d affected by zeta iteration i, is: d = log10(i^3)
- BigDecimal p = new BigDecimal(10).pow(rounds);
- BigDecimal root = Zeta.cuberoot(p, 100, mc);
- long r = root.round(new MathContext(1)).longValue();
- System.out.println("Approximating the riemann zeta function to " + rounds + " decimals ("+r+" rounds).\n");
- BigDecimal zeta = Zeta.zeta(x, r, mc);
- System.out.println("Finding best match...");
- BigDecimal smallestDev = new BigDecimal(Integer.MAX_VALUE);
- BigDecimal val = new BigDecimal(0);
- int bestMatch = -1;
- int pi = 1;
- for(int P=0;P<51;P++){
- PiPow = PI.pow(P);
- for(int i=0;i<5000;i++){
- BigDecimal exact = PiPow.divide(new BigDecimal(i+1),mc);
- BigDecimal testDev = (zeta.subtract(exact,mc)).abs();
- if(testDev.compareTo(smallestDev) == -1){
- bestMatch=i;
- smallestDev=testDev;
- val = exact;
- pi=P;
- }
- }}
- smallestDev = smallestDev.setScale(rounds, RoundingMode.CEILING);
- zeta = zeta.setScale(rounds, RoundingMode.CEILING);
- val = val.setScale(rounds, RoundingMode.CEILING);
- System.out.println("The best approximation is (Pi^"+pi+")/"+(bestMatch+1)+" - with a deviation of " + smallestDev.toString());
- System.out.println(" - Zeta("+x+") = " + zeta.toString());
- System.out.println(" - (Pi^"+pi+")/"+(bestMatch+1)+" = " +val);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement