Computerphile
Computerphile

Reputation: 391

Calculating a series

I am trying to write a program that accepts an integer from the user, then it should calculate this series S = 1/2! - 1/3! + 1/4! – 1/5! + .... all the way to 1/x! where x is the integer taken from the user, I already wrote this code to calculate the factorial of x :

import java.util.Scanner;
public class Factorial {

    public static void main(String args[]){

        Scanner x = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int number = x.nextInt();
        int fact = 1;

        for (int i = 1; i <= number; i++){
            fact = fact*i;
        }

        System.out.println("The factorial of "+number+" is "+fact);
        x.close();
    }
}

but still am not sure how to code the series, any tips would be really appreciated.

Also I am sorry if my code is not organized I don't know how to use stackoverflow tools ;( .

Upvotes: 0

Views: 1588

Answers (2)

cjds
cjds

Reputation: 8426

Ideally, what you want is to separate your code into multiple functions, and think logically.

Since you said you didn't want tips, I'll just try to put you on the right track.


Tip 1:

Separate your code into multiple functions

eg.

public static int factorial(int n){
   int fact = 1;

    for (int i = 1; i <= n; i++){
        fact = fact*i;

    }
    return fact;
}

This allows you to split your code up into manageable chunks. Call each chunk at the appropriate time. This makes your code easier to read and more reusable


Tip 2:

One main class and the other class with functions.

Ideally, you want to create two classes, one which takes input from the user and one which contains all the functions you need. The main class taking the input will create an Object of the other class

public class Factorial{
    public static void main(String args[]){

    Scanner x = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int number = x.nextInt();
    Series s=new Series(number);
    s.print();
    x.close();
}

And in Series.java

public class Series{
   int output;
   int input;

   Series(int i){
      input=i;
      //..here you calculate output
   }
   public int factorial(int n){
        //.... the code 
   }

  public void print(){
     System.out.println("The calculation of " + input + " is " + output);
  }


}

Tip 3:

Make a nice simple function to calculate the output. Sum up all your factorials over time

 for (int i = 2; i <= input; i++) {
        //if its even
        if(i%2==0)
           output = output + 1.0 / factorial(i);
        else
           output = output - 1.0 / factorial(i);
 }

Add the following to your constructor and you'll have a well built Java program


Tip 4:: These sums are going to be decimals, not integers so you need to replace all your ints with doubles

Upvotes: 2

Michel Billaud
Michel Billaud

Reputation: 1826

First, you have to compute a sum of terms. The standard pattern is like

double sum = 0;
for (int i = first; i <= last; i++) {
   sum += term(i);
}

then remark that

  1. the first term is term(2) = +1/2!
  2. the second term is term(3) = -1/3! = -term(2)/3
  3. the third term is +1/4! = -term(3)/4
  4. etc.

So you'll notice that each term can be easily obtained from the previous one.

This leads to

double sum = 0;
double term = (some value);
for (int i = first; i <= last; i++) {
   term = (some expression involving i and previous term);
   sum += term;
}

Exercise left as, huh, an exercise ?

Upvotes: 0

Related Questions