I am trying to calculate pi with the Nilakantha method. Whenever I run this program I get -Infinity if I input 1 and anything else I get NaN.
I am trying to modify my program that uses the Leibniz method, and I'm very new to java.
I appreciate all help!
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of calculations you would like to do");
long no = Long.parseLong(reader.readLine());
long cycle = 0;
long w = 2;
long x = 3;
long y = 4;
long z = 4;
long odd=1;
long i=1;
long a = 1;
long b = 1;
double pi= 0.0;
for(;i<=no;i++)
{
a = w*x*y;
b = x*y*z;
double currentTerm=0.0;
if (i%2==0)
{
currentTerm=(double)4/a;
cycle = cycle+1;
w = w+1;
x = x+1;
y = y+1;
}
else
{
currentTerm=(double)-4/b;
cycle = cycle+1;
x = x+1;
y = y+1;
z = z+1;
}
odd=odd+2;
pi = pi+currentTerm;
}
System.out.println("You calculated that pi is");
System.out.println(pi);
System.out.println(3.1415926535897932);
System.out.println("Pi is actually");
double error = pi/3.1415926535897932;
if(error >= 1) {
double bigerror=2-error;
System.out.println("Your accuracy is");
System.out.println(bigerror*100);
System.out.println("percent");
System.out.println(cycle);
}
else {
System.out.println("Your accuracy is");
System.out.println(error*100);
System.out.println("percent");
System.out.println(cycle);
}
}
}
On your first iteration a and b are both zeros.
I don't think you have your initialization part correct.
http://helloacm.com/two-simple-equations-to-compute-pi/
Here I see that j starts from 2.
You have zeroes.
Make sure you implement the algorithm correctly.
Here is your code corrected.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MainProgram {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of calculations you would like to do");
long no = Long.parseLong(reader.readLine());
long step = 0;
double ans = 3;
long j = 2;
double pi = 0.0;
while (true) {
step++;
if ((step % 2) == 1) {
ans += 4.0 / (1.0 * j * (j + 1) * (j + 2));
} else {
ans -= 4.0 / (1.0 * j * (j + 1) * (j + 2));
}
j += 2;
pi = ans;
if (step >= no)
break;
}
System.out.println("You calculated that pi is");
System.out.println(pi);
System.out.println("Pi is actually");
System.out.println(3.1415926535897932);
double error = pi / 3.1415926535897932;
if (error >= 1) {
double bigerror = 2 - error;
System.out.print("Your accuracy is: ");
System.out.print(bigerror * 100);
System.out.println(" percent");
System.out.println(step);
} else {
System.out.print("Your accuracy is: ");
System.out.print(error * 100);
System.out.println(" percent.");
System.out.println(step);
}
}
}
Related
I tried to recreate this formula i am sure it's correct but the problem it gives me infinity and i need your help to transform it to BigInteger please help if possible
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
double X = Input.nextDouble();
double Y = Input.nextDouble();
double Ans = 0;
for (int I = 1; I <= 20; I++) {
for (int J = 1; J <= 20; J++) {
Ans += Math.log10(1 + Math.exp(Math.pow(-1, I + J) * (((Math.pow(I, 2) * X)) + (Math.pow(J, 2)) * Y)));
}
}
System.out.println(Ans);
}
}
I'm getting a "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at WeatherAnalysis.main(WeatherAnalysis.java:37)" Im not sure what is wrong at line 37, date[CurrentLine][0] = (int)fullDate % 100;. Trying to use .txt file to output weather patterns from 1941 to 2013.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class WeatherAnalysis {
public static void main(String[] args) throws FileNotFoundException {
int NumberOfDays = 0;
String station;
File f = new File("PortlandWeatherCut.txt"); //Import text file.
Scanner input= new Scanner(f);
input.nextLine();
input.nextLine();
while (input.hasNextLine()) { //Assign number of days to be
NumberOfDays++;
input.nextLine();
}
double[][] date = new double[NumberOfDays][3];
double[] prcp = new double[NumberOfDays];
double[] snow = new double[NumberOfDays];
double[] snwd = new double[NumberOfDays];
double[] tmax = new double[NumberOfDays];
double[] tmin = new double[NumberOfDays];
int CurrentLine = 0;
double fullDate = 0.0;
File g = new File("PortlandWeather.txt"); //Import text file.
Scanner weather= new Scanner(g);
weather.nextLine(); weather.nextLine();
while(weather.hasNextLine()) { //variables and converts to inches.
station = weather.next();
fullDate = weather.nextDouble();
date[CurrentLine][0] = (int)fullDate % 100;
date[CurrentLine][1] = ((int)fullDate % 10000) / 100;
date[CurrentLine][2] = (int)fullDate / 10000;
prcp[CurrentLine] = weather.nextDouble()* .00393701;
snow[CurrentLine] = weather.nextDouble()* 0.0393701;
snwd[CurrentLine] = weather.nextDouble()* 0.0393701;
tmax[CurrentLine] = (weather.nextDouble() / 10.0) * (9.0/5.0) + 32.0;
tmin[CurrentLine] = (weather.nextDouble() / 10.0) * (9.0/5.0) + 32.0;
CurrentLine++;
weather.nextLine();
}
int indexPoint = 0;
int nextPoint = 1;
int endPoint = 0;
for (int x = 1; x < date.length; x++){
//loop that calculates array section to be averaged,
//uses a method to average them, then prints a table
do {
nextPoint++;
endPoint++;
}
while(((int)date[nextPoint - 1][02] / 10) == ((int)date[nextPoint][1] / 10));
if ( nextPoint < date.length) {
System.out.printf("%4.0f's Average Max Temp = %4.1f\tAverage Min Temp = %4.1f\n",
date[indexPoint][02],arrayAvg(tmax, indexPoint, endPoint),
arrayAvg(tmin, indexPoint, endPoint));
indexPoint = nextPoint;
} else
System.out.println();
}
}
public static double arrayAvg(double a[], int fromIndex, int toIndex) {
//Averages the values between the entered parameters and returns it.
double sum = 0;
double divisor = 0;
for (int i = fromIndex; i < toIndex; i++){
if (a[i] == 393.6616299) {
divisor++;
} else {
sum += a[i];
divisor++;
}
}
double average = sum / divisor;
return average;
}
}
Seems like your issue is at line 37. I think that is the
date[currentline][0] = (int)fullDate%100
Inside of your while loop. The loop continues above the NumberOFdAYS that the date 2-dimensional array was created with, therefore it throws out of bout.
My first advise is to break down this monster function into smaller function (Look up Single Responsibility principle).
My second advise is to do TDD (Test driven development)
But if you want a quick and dirty debug, do the following at the beginning of your while loop:
System.out.printf("Current line = %d, does weather has next line? = %b", CurrentLine, weather.hasNextLine)
This will show you all the values of currentLine before the exception and shows the value of your boolean.
I'm using Wallis' method to calculate pi, and I think I did it right. At least I thought I did anyway. I think the problem (output is 0)has to do with rounding and remainders, though I can't be sure. Here's the code:
import java.util.Scanner;
public class WallisPi {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = 2;
int b = 3;
int c = 1;
int pi = 0;
double acc = 0.0;
int n = scan.nextInt();
scan.close();
for (int i = 0; i <= n; i++) {
pi = (2 / 3) * c;
if (a > b) {
b += 2;
} else {
a += 2;
}
c = a / b;
}
pi *= 4;
System.out.println("The approximation of pi is " + pi + ".");
acc = Math.PI - pi;
System.out.println("It is " + acc + " off.");
}
}
Since posting this I've made some changes to the code, though it's still not quite functional. I get 2.666..., so there's something else at work here as well.
import java.util.Scanner;
public class WallisPi {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a = 2.0;
double b = 3.0;
double c = 1.0;
double pi = 0;
double acc = 0.0;
int n = scan.nextInt();
scan.close();
for (int i = 0; i <= n; i++) {
pi = (2.0 / 3.0) * c;
if (a > b) {
b += 2;
} else {
a += 2;
}
c = a / b;
}
pi *= 4;
System.out.println("The approximation of pi is " + pi + ".");
acc = Math.PI - pi;
System.out.println("It is " + acc + " off.");
}
}
int a=2;
int b=3;
double pi=2;
for(int i=0;i<=n;i++){
pi *= (double)a/(double)b;
if(a>b){
b+=2;
} else {
a+=2;
}
}
pi*=2;
Using n = 4000 yields 3.141200
Here's the whole program, fixed:
import java.util.Scanner;
public class WallisPi {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
double pi = 2;
int a = 2;
int b = 3;
for (int i = 0; i <= n; i++){
pi *= (double) a / (double) b;
if (a > b) {
b += 2;
} else {
a += 2;
}
}
pi *= 2;
double acc = Math.PI - pi;
System.out.println("The approximation of pi is " + pi + ".");
System.out.println("It is " + acc + " off.");
}
}
Since your varibles are ints, all your divisions are integer divisions, omitting the fraction (and preserving only the whole part of the result). For accurate results, you should define your variables as doubles:
double a=2;
double b=3;
double c=1;
double pi=0;
I've been staring at this for a few hours, and I can't seem to figure out why my outputs are coming out too small. My outputs do what you'd expect, level off, they just level off to the wrong value. I'm very certain the error lies within the while loop of my main method, or the nested for loop, but I'll go ahead and post everything incase it is hiding somewhere else.
The purpose of this program is to estimate the value of e^x by summing 1 + x + x^2/2! + x^3/3! + ... + x^n/n!. It needs to output the sum for n take to each value between 1 and 10, along with the values of 50 and 100. So 12 outputs all together.
import java.io.*;
import java.text.DecimalFormat;
public class Ch3Ex7
{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static double x = 0;
static DecimalFormat df = new DecimalFormat("0.00");
public static void main (String[] args)
{
mainMenu();
int counter = 1;
double heldx = x;
double holderx = 0;
double denom = 1;
double printx = 0;
double f = 1;
while (counter != 100)
{
x = x*heldx;
denom++;
for(int i = 2; i<=denom; i++)
{
f = f*i;
}
holderx = holderx + x/f;
if ((counter > 0 && counter <= 10) || (counter == 50))
{
printx = 1 + heldx + holderx;
System.out.println(df.format(printx));
}
counter++;
}
System.out.println(df.format(printx));
f = 0;
x = 0;
counter = 1;
denom = 1;
callMain();
}
public static void mainMenu()
{
try
{
System.out.println("Requesting user input, press 0 to leave");
x = Integer.parseInt(br.readLine());
space();
if (x == 0)
{
System.exit(0);
}
}
catch(IOException ioe) {}
}
public static void callMain()
{
String[] x = {"A" , "B"};
Ch3Ex7.main(x);
}
public static void space()
{
System.out.println();
}
}
The problem is with your "f" variable. You're trying to calculating the value every time, but you end up starting with your old value.
Get rid of that for loop and add f *= denom; in its place.
import java.io.*;
public class TestCaseAbcd {
public static void main(String args[]) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(
float x0;
float a, c, mod;
int num, ch = 0;
double[] rNumbers;
double[] rTemp;
System.out.println("Enter the SEED value: ");
System.out.println("x0 ");
x0 = Float.parseFloat(stdin.readLine());
System.out.println("Enter the multiplier's value:");
System.out.println("a: ");
a = Float.parseFloat(stdin.readLine());
System.out.println("Enter the value of increment c and modulus m: ");
System.out.println("c: ");
c = Float.parseFloat(stdin.readLine());
System.out.println("m: ");
mod = Integer.parseInt(stdin.readLine());
System.out.println("How many random nunbers u need? ");
num = Integer.parseInt(stdin.readLine());
rNUmbers = new double[num];
rTemp = new double[rNumbers.length];
rTemp[0] = x0;
for (int i = 0; i < num; i++)
{
if(i + 1 != num)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
else
{
break;
}
}
for (int i = 0; i < rNumbers.length; i++)
{
if (i + 1 != num)
{
rNumbers[i] = rTemp[i] / mod;
}
else
{
break;
}
}
System.out.println("The PSEUDO random numbers are: ");
for (int i = 0; i < rNumbers.length; i++)
{
System.out.println(rNumbers[i]);
}
double firstNum = rNumbers[0];
System.out.println("1. Select Mutant 1 ");
System.out.println("2. Select Mutant 2 ");
System.out.println("3. Exit ");
}
}
In the above code, the expected output should start from: 0.68.
But, instead it started from:
0.37.
In fact, even after I changed the following code:
rTemp[i+1] = ( ( (rTemp[i]*a) + c ) % mod);
to:
rTemp[i+1] = ( ( (rTemp[i]/a) + c ) % mod);
The output still started from 0.37.
The input values are:
x0 = 37
a = 7
c = 9
m = 100
Please help me in analyzing the code so as that the output shouldn't start with 0.37.
Summary of the problem: the code is producing the same number i.e. 0.37 no matter what the equation stated above in the code is modified to.
You're setting the first value in rTemp to 37, then you start printing from the first value. It only makes sense that the first value printed would be 0.37.
x0 = 37
mod = 100
...
rTemp[0] = x0;
...
rNumbers[i] = rTemp[i] / mod;
...
System.out.println(rNumbers[i]);
To achieve the output you're looking for, change this:
rTemp[0] = x0;
for (int i = 0; i < num; i++)
{
if(i + 1 != num)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
else
{
break;
}
}
to this:
rTemp[0] = (((x0 * a) + c) % mod);
for (int i = 0; i < num-1; i++)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
On a side note, if all you really want is an array of psudo-random doubles, it's easier to do something like this:
Random rnd = new Random(seed);
double[] rNumbers = new double[num];
for(int i = 0; i < num; i++)
rNumbers[i] = (double)rnd.nextInt(100) / 100;
For any given seed, you'll get the same array of doubles every time.