Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Summing n terms of a Fibonacci Sequence

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Summing n terms of a Fibonacci Sequence

     
     
    public class FibonacciSequence extends Sequence implements Sequenceable{
     
        private double secondTerm;
     
        public FibonacciSequence(){
            super(1);
            this.secondTerm = 1;
        }
        /**
         * 
         * @param first first term of fib sequence
         * @param second second term of fib sequence
         */
        public FibonacciSequence(double first, double second){
     
            super(first);
            this.secondTerm = second;
        }
    /**
     * 
     * @param n 
     * @return 
     */
        @Override
        public double getNthTerm(int n) {
     
            double term1 = super.getFirstTerm();
            double term2 = this.secondTerm;
            double nTerm = 0;
     
          if(n == 1){
              return term1;
          }
          else if (n == 2){
              return term2;
          }
          else{
              for(int i = 3;i <= n ; i++){
                  nTerm = term1 + term2;
                  term1 = term2;      
                  term2 = nTerm;
     
     
              }
             return nTerm;
          }
     
     
     
         }
     
     
    /**
     * 
     * @param n 
     * @return 
     */
        @Override
        public double sumNTerms(int n) {
           throw new UnsupportedOperationException("Not supported yet.");
     
     
     
    }
    }

    I know the Fibonacci sequence is firstTerm + secondTerm = thirdTerm, secondTerm + thirdTerm = fourthTerm and so on. I am confused on how to write the syntax to sum n number of terms.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Summing n terms of a Fibonacci Sequence

    recursion is common in a solution to this type of problem
    There seems to be a starting point to the system, and the same process repeated over and over without changing the process.
    The starting point defines your base case(s) and the repeated process defines what is to be updated causing you to eventually reach a base case.

Similar Threads

  1. Recursive Fibonacci sequence optimization
    By aesguitar in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 24th, 2012, 08:49 AM
  2. The Fibonacci sequence
    By Ryuk_93 in forum Android Development
    Replies: 1
    Last Post: March 26th, 2012, 11:56 AM
  3. Newbie: needs meaning of terms (dictionary)
    By boyscout in forum Member Introductions
    Replies: 1
    Last Post: March 29th, 2011, 05:34 AM
  4. Summing a 4X4 Matrix in JOptionPane
    By Java Neil in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 15th, 2011, 09:15 AM
  5. Im so confused! Counting and Summing
    By captiancd89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 28th, 2010, 06:49 PM