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

  • Home
  • SEARCH
  • 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 289101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:52:36+00:00 2026-05-12T05:52:36+00:00

Can someone hep me find an algorithm for a recursive function func(int k) to

  • 0

Can someone hep me find an algorithm for a recursive function func(int k) to return 3^k in only n+1 calls where k is in the range [ 3^n, 3^(n+1) )

For example, the function should return 3^1 or 3^2 in 1 call, 3^3, 3^4, .., 3^8 in 2 calls, 3^9, 3^10 .. in 3 calls and so on.

  • 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-12T05:52:36+00:00Added an answer on May 12, 2026 at 5:52 am

    Here is the algorithm in untested C/C++:

    int 3pow(x)
    {
        switch(x)
        {
            case 1: return 3;
            case 2: return 9;
            case 3: return 27;
        }
    
        int remain  = x % 3,
            recur   = 3pow((x-remain)/3),
            combine = recur * recur * recur;
    
        switch (remain)
        {
            case 0:  return combine;
            case 1:  return combine * 3;
            default: return combine * 9;
        }
    }
    

    (I have not compiled, run, or otherwise tested this code. There may be syntax errors and other bugs. But it’s sufficient to get the point across.)

    What’s different about this algorithm is that it recurses by dividing by 3 instead of 2. This function is rather ugly by necessity, as abstracting away some of the pattern would likely involve more recursion. Still, this algorithm is now O(log3n). Here’s a table of a given x from 1-50 and the number of recursive calls required (as a two-element lisp list):

    (1  1) (2  1) (3  1) (4  2) (5  2) (6  2) (7  2) (8  2) (9  2) (10 2)
    (11 2) (12 3) (13 3) (14 3) (15 3) (16 3) (17 3) (18 3) (19 3) (20 3)
    (21 3) (22 3) (23 3) (24 3) (25 3) (26 3) (27 3) (28 3) (29 3) (30 3)
    (31 3) (32 3) (33 3) (34 3) (35 3) (36 4) (37 4) (38 4) (39 4) (40 4)
    (41 4) (42 4) (43 4) (44 4) (45 4) (46 4) (47 4) (48 4) (49 4) (50 4)
    (51 4) (52 4) (53 4) (54 4) (55 4) (56 4) (57 4) (58 4) (59 4) (60 4)
    (61 4) (62 4) (63 4) (64 4) (65 4) (66 4) (67 4) (68 4) (69 4) (70 4)
    (71 4) (72 4) (73 4) (74 4) (75 4) (76 4) (77 4) (78 4) (79 4) (80 4)
    (81 4) (82 4) (83 4) (84 4) (85 4) (86 4) (87 4) (88 4) (89 4) (90 4)
    (91 4) (92 4) (93 4) (94 4) (95 4) (96 4) (97 4) (98 4) (99 4) (100 4)
    

    You can see that even as we get up over 100 it only take 4 recursive calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 129k
  • Answers 129k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can just drop the asp:Login control in your page,… May 12, 2026 at 5:53 am
  • Editorial Team
    Editorial Team added an answer If what you want to accomplish is have the Editor… May 12, 2026 at 5:53 am
  • Editorial Team
    Editorial Team added an answer You could create another table that has the auto-increment field.… May 12, 2026 at 5:53 am

Related Questions

Can someone show me how to implement a recursive lambda expression to traverse a
Can someone explain the mechanics of a jump table and why is would be
Can someone give an example of a good time to actually use unsafe and
Can someone describe what a symbol table is within the context of C and
Can someone explain to me the advantages of using an IOC container over simply

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.