Java: Arithmetic and Geometric Progression (Mathematics)
Tuesday, 13. June 2006, 01:53:11
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:
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!)
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:
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!
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!)
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!








Anonymous # 12. January 2009, 12:57
I NEED QUESTIONS AND SOLUTIONS ON GEOMETRIC PROGRESSION AND ARITHEMETIC PROGRESSION
Anonymous # 5. March 2009, 07:34
i kinda having a problem in implementing geometric progression!! can you pls. help me??