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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:19:59+00:00 2026-05-12T06:19:59+00:00

The following program, was compiled and tested, it sometimes return the result, and sometimes

  • 0

The following program, was compiled and tested, it sometimes return the result, and sometimes fills the screen with

java.lang.StackOverflowError
at scala.BigInt$.apply(BigInt.scala:47)
at scala.BigInt.equals(BigInt.scala:129)
at scala.runtime.BoxesRunTime.equals(Unknown Source)
at bigint$.factorial(fact2.scala:3)
at bigint$.factorial(fact2.scala:3)
...

The program:

object bigint extends Application {
  def factorial(n: BigInt): BigInt = if (n == 0) 1 else n * factorial(n-1)
  println("4391! = "+factorial(4391))
}

My questions:

  • Is it because there is a stack overflow on the JVM, which sometimes happens and sometimes doesn’t?
  • Is this nondeterministic behavior considered a bug?
  • I assume Scala did not tail-recursed this? how can I make it tail-recurse this?

Details:

Scala compiler version 2.7.5.final —
Copyright 2002-2009, LAMP/EPFL Scala
code runner version 2.7.5.final —
Copyright 2002-2009, LAMP/EPFL

java version “1.6.0_0” OpenJDK
Runtime Environment (build
1.6.0_0-b11) OpenJDK Client VM (build 1.6.0_0-b11, mixed mode, sharing)

Ubuntu 2.6.24-24-generic

  • 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-12T06:19:59+00:00Added an answer on May 12, 2026 at 6:19 am

    Tail-call optimization will only work in Scala if the recursive call is the last statement in the function. It’s very limited. The Scala book says:

    […] tail-call optimization is
    limited to situations in which a
    method or nested function calls itself
    directly as its last operation,
    without going through a function value
    or some other intermediary.

    In your case, the recursive call is part of a larger expression, and is not itself the very last operation – the last operation here is the multiplication.

    This article demonstrates how to make it work:

    class Factorial {
      def factorial(n: Int): Int = {
        def factorialAcc(acc: Int, n: Int): Int = {
          if (n <= 1) acc
          else factorialAcc(n * acc, n - 1)
        }
        factorialAcc(1, n)
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think you're being over-cautious, but I do think… May 12, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer Let's say you want to copy directory trunk/project of rep_a… May 12, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer Just curious why are you saving this in the base… May 12, 2026 at 4:25 pm

Related Questions

I wrote the following C++ program class MyClass { public: int i; int j;
When I attempt to debug a simple program with gdb on cygwin I get
There's a class which is compiled into a dll //HeaderFile.h //version 1.0 class __declspec(dllexport)
I recently developed an interop user control in .NET (Visual Studio 2008, project targetting

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.