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 Monday, January 12, 2009 12:57:47 PM

Anonymous writes: I NEED QUESTIONS AND SOLUTIONS ON GEOMETRIC PROGRESSION AND ARITHEMETIC PROGRESSION

Anonymous Thursday, March 5, 2009 7:34:44 AM

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

Anonymous Saturday, December 5, 2009 5:31:43 PM

Anonymous writes: geometric is when you have a set of data that can be multiplyed.

Anonymous Saturday, July 16, 2011 1:01:25 AM

cVsu student... writes: what if we try to reverse the formula.... it's hard to find the answer in tht wat.. i can't do my assignment....

Anonymous Sunday, August 28, 2011 6:28:50 AM

mohd atif writes: its my first tym on java ,tell me the braod way to get te the right kowledge on java

Anonymous Tuesday, October 18, 2011 6:18:05 AM

GRSMND writes: INFORMATION TECHNOLOGY IS TOO HARD

Anonymous Tuesday, October 18, 2011 6:19:17 AM

Anil writes: java programmes are too difficult

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.)

If you can't read the words, press the small reload icon.


Smilies