Skip navigation.

Development and miscellaneous!

Java: Arithmetic and Geometric Progression (Mathematics)

, ,

For those who doesn't know what this means in mathematics, please see Arithmetic Progression(wikipedia.org) and Geometric Progression(wikipedia.org).
A quick summary could be: when you have a string of numbers which is either the previous number added with a number d, or when the previous number is multipled with a number k.

This class will enable you to find any number in such a sequence which you specify, as well as finding the sum of such a sequence:
class Aritmetic {
    long a;
    double d;      
    public Aritmetic(long a, double d) {
        this.a = a;
        this.d = d;       
    } 
    public double calcTerm(long i) { return (a + ((i - 1)*d)); }
    public double calcSum(long i) { return (i * (calcTerm(1L) + calcTerm(i))/2); }
}
class Geometric {
    long a;
    double k;    
    public Geometric(long a, double k) {
        this.a = a;
        this.k = k;        
    } 
    public double calcTerm(long i) { return (Math.pow(k, (i - 1))*a); }
    public double calcSum(long i) { return (a * ((Math.pow(k, (i)) - 1)/(k - 1))); }
}

Here you have two classes, one for arithmetic and one for geometric progression. They will both have to be initiated with two arguments. The first argument, a, is the starting number of the sequence. The second argument, d or k, is the difference in an arithmetic sequence and the common ratio in the geometric sequence. (If that sounded insane to you, please see the previously mentioned wikipedia entries for a better explanation!) :wink:
The method calcTerm with calculate the value of the i-th term. That is, the number which the sequence gives at position i.
The method calcSum will calculate the value of the sum from the starting number to the i-th term.

Example Usage:
Arithmetic atest = new Arithmetic(1, 2);
System.out.println("Arithmetic Term: " + atest.calcTerm(64));
System.out.println("Arithmetic Sigma: " + atest.calcSum(64));

Geometric gtest = new Geometric(1, 2);
System.out.println("Geometric Term: " + gtest.calcTerm(64));
System.out.println("Geometric Sigma: " + gtest.calcSum(64));

This little example happens to show why the mathematician who invented chess wanted his rice beans to double, as opposed to increase by two per square on the chess board!

Java: Random element from array.Java: (slightly advanced) Optimized Fermat Primality Test

Comments

Anonymous 12. January 2009, 12:57

Anonymous writes:

I NEED QUESTIONS AND SOLUTIONS ON GEOMETRIC PROGRESSION AND ARITHEMETIC PROGRESSION

Anonymous 5. March 2009, 07:34

Anonymous writes:

i kinda having a problem in implementing geometric progression!! can you pls. help me??

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

Type the two words displayed in the image below:


Smilies

July 2009
S M T W T F S
June 2009August 2009
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