Having issue with Tail Recursion function

We have return code as

import scala.annotation.tailrec
object TailRecursion extends App {
val input1 = args(0).toInt
val input2 = args(1).toInt
def gcd

val output = gcd(input1, input2)
println(output)
}

need out as 7, please guide us

Hi, welcome to the community.

First of all, for future questions, you can format your code like this:

```scala
// Your code here
```

Second, it would help if you can be more clear about what is your question.
What was your problem? What did you try?

For what I can see you need to define the gdc method.
And the implementation has to be tailrec.
So before we can help you, first of all, define what should gdc do, what are their inputs and what would be the output.
And second, show some effort to solve the problem yourself.

1 Like

Written fresh code for Tail Recursion Function but it is not working as expected , getting error. Please help us

    def factorialWithAccumulator(accumulator: Int, number: Int) : Int = {
       if (number == 1)
        return accumulator
       else
        factorialWithAccumulator(accumulator * number, number - 1)
      }
      factorialWithAccumulator(1, number)

    import scala.annotation.tailrec
    object TailRecursion2 extends App {
     val input1 = args(0).toInt
      def factorial(number: Int): Int = {
      @tailrec
      def iter(x: Int, result: Int): Int =
    if (x == input1) result
    else iter(x - 1, result * x)
      iter(number, input1)
    }
     println(factorial(input1))
    }

error as

$ scalac TailRecursion.scala
TailRecursion.scala:1: error: expected class or object definition
def factorialWithAccumulator(accumulator: Int, number: Int) : Int = {
^
TailRecursion.scala:7: error: expected class or object definition
factorialWithAccumulator(1, number)
^
two errors found

Well the error is pretty clear, you can not define a method as a top level definition.
It has to be done inside a class or an object.

BTW, you do not use that factorialWithAccumulator in any place and is very similar to the factorial defined inside the object (but that one has a bug in the implementation).

Also, do not use return it is not needed and in your case it will break the code and produce a wrong answer.

Bit confused , you mean code will be

import scala.annotation.tailrec
object TailRecursion2 extends App {
 val input1 = args(0).toInt
 def factorial(number: Int): Int = {
  @tailrec
def factorialWithAccumulator(accumulator: Int, number: Int) : Int = {
   if (number == 1)
    return accumulator
   else
    factorialWithAccumulator(accumulator * number, number - 1)
  }
  factorialWithAccumulator(1, number)
}
 println(factorial(input1))
}

Yeah, but as I said, do not use return you do not need it and it changes the semantics of the code.
Take a look to this.

So the final code looks like this:

import scala.annotation.tailrec

object TailRecursion2 extends App {
  def factorial(number: Int): Int = {
    @tailrec
    def factorialWithAccumulator(accumulator: Int, number: Int) : Int =
      if (number == 1) accumulator
      else factorialWithAccumulator(accumulator * number, number - 1)

    factorialWithAccumulator(1, number)
  }

  val input1 = args(0).toInt
  println(factorial(input1))
}

Thanks it is working and previous code also working fine … thanks a lot

I still get the below error . Could you please help

TailRecursion.scala:1: error: expected class or object definition
def factorialWithAccumulator(accumulator: Int, number: Int) : Int = {
^
TailRecursion.scala:7: error: expected class or object definition
factorialWithAccumulator(1, number)
^
two errors found

Code

import scala.annotation.tailrec

object TailRecursion2 extends App {
def factorial(number: Int): Int = {
@tailrec
def factorialWithAccumulator(accumulator: Int, number: Int) : Int =
if (number == 1) accumulator
else factorialWithAccumulator(accumulator * number, number - 1)

factorialWithAccumulator(1, number)

}

val input1 = args(0).toInt
println(factorial(input1))

I can’t reproduce the error with a file that only has the snippet of my last response.
Maybe you leave something from your previous code there?

HI … this is what i have given …

def factorialWithAccumulator(accumulator: Int, number: Int) : Int = {
if (number == 1)
return accumulator
else
factorialWithAccumulator(accumulator * number, number - 1)
}
factorialWithAccumulator(1, number)
import scala.annotation.tailrec

object TailRecursion2 extends App {
def factorial(number: Int): Int = {
@tailrec
def factorialWithAccumulator(accumulator: Int, number: Int) : Int =
if (number == 1) accumulator
else factorialWithAccumulator(accumulator * number, number - 1)

factorialWithAccumulator(1, number)

}

val input1 = args(0).toInt
println(factorial(input1))
}

@SV84 re-read carefully all the messages in this thread, it should be obvious what the problem is and what the solution is.

def factorialWithAccumulator(accumulator: Int, number: Int) : Int = {
if (number == 1)
return accumulator
else
factorialWithAccumulator(accumulator * number, number - 1)
}
factorialWithAccumulator(1, number)
import scala.annotation.tailrec

object TailRecursion2 extends App {
def factorial(number: Int): Int = {
@tailrec
def factorialWithAccumulator(accumulator: Int, number: Int) : Int =
if (number == 1)
return accumulator
else factorialWithAccumulator(accumulator * number, number - 1)

factorialWithAccumulator(1, number)

}
val input1 = args(0).toInt
println(factorial(input1))
}

i tried this as well… can you pls tell where have i gone wrong. Not able to figure out

Are you getting an error message? If so, what it does it say? Or if you’re getting some other kind of unexpected behavior, what is it?

It seems to me that your code works:

scala 2.13.3> TailRecursion2.main(Array("5"))
120

TailRecursion.scala:1: error: expected class or object definition
def factorialWithAccumulator(accumulator: Int, number: Int) : Int = {
^
TailRecursion.scala:7: error: expected class or object definition
factorialWithAccumulator(1, number)
^
two errors found

You should put the definition of factorialWithAccumulator inside the object TailRecursion2. At present it is at top level, and scala does not have top level functions.

regards,
Siddhartha

Yes got it, Thankyou Sir!