File: partialsums.java

package info (click to toggle)
groovy2 2.2.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 23,916 kB
  • sloc: java: 136,570; xml: 948; sh: 486; makefile: 67; ansic: 64
file content (46 lines) | stat: -rw-r--r-- 1,622 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* The Computer Language Shootout
   http://shootout.alioth.debian.org/
   contributed by Josh Goldfoot
   modified by Isaac Gouy
*/

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class partialsums {
   private static final double twothirds = 2.0/3.0;
   private static final NumberFormat formatter = new DecimalFormat("#0.000000000");


   public static void main(String[] args) {
      int n = Integer.parseInt(args[0]);

      double a1 = 0.0, a2 = 0.0, a3 = 0.0, a4 = 0.0, a5 = 0.0;
      double a6 = 0.0, a7 = 0.0, a8 = 0.0, a9 = 0.0, alt = -1.0;

      for (int k=1; k<=n; k++){
         double k2 = (double)k * (double)k, k3 = k2 * (double)k;
         double sk = Math.sin(k), ck = Math.cos(k);
         alt = -alt;

         a1 += Math.pow(twothirds,k-1);
         a2 += 1.0/Math.sqrt(k);
         a3 += 1.0/(k*(k+1.0));
         a4 += 1.0/(k3 * sk*sk);
         a5 += 1.0/(k3 * ck*ck);
         a6 += 1.0/k;
         a7 += 1.0/k2;
         a8 += alt/k;
         a9 += alt/(2.0*k -1.0);
      }
      System.out.println(formatter.format(a1) + "\t(2/3)^k");
      System.out.println(formatter.format(a2) + "\tk^-0.5");
      System.out.println(formatter.format(a3) + "\t1/k(k+1)");
      System.out.println(formatter.format(a4) + "\tFlint Hills");
      System.out.println(formatter.format(a5) + "\tCookson Hills");
      System.out.println(formatter.format(a6) + "\tHarmonic");
      System.out.println(formatter.format(a7) + "\tRiemann Zeta");
      System.out.println(formatter.format(a8) + "\tAlternating Harmonic");
      System.out.println(formatter.format(a9) + "\tGregory");
   }
}