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 5953833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:52:58+00:00 2026-05-22T17:52:58+00:00

I wrote a small piece of code that I believe should have succeeded if

  • 0

I wrote a small piece of code that I believe should have succeeded if tail recursion was optimized, however it blew up the stack. Should I conclude PHP does not optimize tail recursion?

function sumrand($n,$sum) {
    if ($n== 0) {
        return $sum;
    }
    else {
        return (sumrand($n-1,$sum+rand(0,1)));
    }
}
echo sumrand(500000,0)."\n";
  • 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-22T17:52:59+00:00Added an answer on May 22, 2026 at 5:52 pm

    Here are the generated opcodes for that (sorry for the strange representation):

    Global
    -------------------------------------------------------------------------------
    BCDCAC 0003: NOP                  ()
    BCDD24 0012: SEND_VAL             (CONST: "500000")
    BCDD9C 0012: SEND_VAL             (CONST: NULL)
    BCDE14 0012: DO_FCALL             (CONST: "sumrand") -> VAR 0
    BCDE8C 0012: CONCAT               (VAR 0, CONST: "\n") -> TMP_VAR 1
    BCDF04 0012: ECHO                 (TMP_VAR 1)
    BCDF7C 0014: RETURN               (CONST: "1")
    
    Functions
    -------------------------------------------------------------------------------
    sumrand (17 op)
    BCFABC 0003: RECV                 (CONST: "1") -> CV 0 ($n)
    BCFB34 0003: RECV                 (CONST: "2") -> CV 1 ($sum)
    BCFBAC 0004: IS_EQUAL             (CV 0 ($n), CONST: NULL) -> TMP_VAR 0
    BCFC24 0004: JMPZ                 (TMP_VAR 0, &(BCFD18+6))
    BCFC9C 0005: RETURN               (CV 1 ($sum))
    BCFD14 0006: JMP                  (&(BD01C8+10))
    BCFD8C 0008: INIT_FCALL_BY_NAME   (NULL, CONST: "sumrand")
    BCFE04 0008: SUB                  (CV 0 ($n), CONST: "1") -> TMP_VAR 1
    BCFE7C 0008: SEND_VAL             (TMP_VAR 1)
    BCFEF4 0008: SEND_VAL             (CONST: NULL)
    BCFF6C 0008: SEND_VAL             (CONST: "1")
    BCFFE4 0008: DO_FCALL             (CONST: "rand") -> VAR 2
    BD005C 0008: ADD                  (CV 1 ($sum), VAR 2) -> TMP_VAR 3
    BD00D4 0008: SEND_VAL             (TMP_VAR 3)
    BD014C 0008: DO_FCALL_BY_NAME     () -> VAR 4
    BD01C4 0008: RETURN               (VAR 4)
    BD023C 0010: RETURN               (CONST: NULL)
    

    So, no, it certainly doesn’t seem so.

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

Sidebar

Related Questions

I wrote small piece of code which should toggle reporting box on my page.
I wrote a small piece of code that rapidly read and write to a
I've wrote a small piece of code that reads the current battery charge/discharge on
here is a small peice off code that i wrote. But I am not
I'm trying to write a small piece of code that merges lines alternatively from
I have written a small piece of code which handles the input of a
I've been working on this small piece of code that seems trivial but still,
I have written a small piece of code to calculate quadratic equations, but if
I am new to JQuery. I wrote a small piece of code ( fiddle
I have some small piece of software that calculates the number of factors of

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.