Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:25:23+00:00 2026-05-29T11:25:23+00:00

I am having a lot of trouble with this basic recursion problem in java;

  • 0

I am having a lot of trouble with this basic recursion problem in java; any pointers would be great.

“Write a static recursive method to print out the nth term of the
geometric sequence: 2, 6, 18, 54.”

From what I can gather, somewhere in the code I should be recursively multiplying something by 3, but I’m struggling to figure out how to do this. I know I need a termination statement, but when does that occur? Do I need a helper method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:25:24+00:00Added an answer on May 29, 2026 at 11:25 am

    A Recursive Function is a function whose implementation references itself. Below is some funny example:

    public class Inception {
       public void dream() {
          boolean enoughDreaming = false;
          //Some code logic below to check if it's high time to stop dreaming recursively
          ...
          ...
    
          if(!enoughDreaming) {
               dream(); //Dream inside a Dream
          }
       }
    }
    

    And the solution for your problem:

    public class GeometricSequence {
        public static void main(String[] args) {
            //Below method parameters - 5 = n, 1 = count (counter), res = result (Nth number in the GP.
            System.out.println(findNthNumber(5, 1, 2));
    
        }
    
        public static int findNthNumber(int n, int count, int res) {
            return ((count == n)) ? res : findNthNumber(n, count+1, res *3);
        }
    }
    

    EDIT:

    The above class uses “int”, which is good only for small numbers (because of Integer Overflow problem). The below class is better for all types/numbers:

    public class GeometricSequence {
        public static void main(String[] args) {
            //Below method parameters - 5 = n, 1 = count (counter), res = result (Nth number in the GP.
            System.out.println(findNthNumber(2000, 1, new BigInteger("2")));
    
        }
    
        public static BigInteger findNthNumber(int n, int count, BigInteger res) {
            return ((count == n)) ? res : findNthNumber(n, count+1, res.multiply(new BigInteger("3")));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been having lot's of trouble with this one. Let's say I had the following
I'm having a lot of trouble writing this regular expression: (?<=\s+|^\s*|\(\s*|\.)(?:item|item1|item2)(?=\s+|\s*$|\s*\)|\.) It works very
So, i'm having a lot of trouble with this little piece of code. An
Hey guys I am having a lot of trouble trying to understand this and
This question might be remedial but I am having a lot of trouble with
I'm having a lot of trouble performing a very basic task: resizing an array.
I thought this would be simple, but I sure am having a lot of
I'm following this tutorial (page 98) and i'm having a lot of trouble. It
I'm having a lot of trouble implementing this deque using a circular array; in
I am having a lot of trouble with this task. Using the Wikipedia definition

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.