How to check whether a number is perfect number or not in a list of integers using scala higher order function

Question:- Can anyone please help me to solve the below problem?

ListMapHigherOrder takes an array of string inputs args from command line. First convert the array of strings args to intList i.e a list of integers. Write a function isPerfectNumber which takes integer input and returns String output . It finds if a number is perfect and returns true if perfect else return false . A perfect number is a number whose sum factors(except for the number itself) is itself (like 6 = 1+2+3). Now write a higher order function myHigherOrderFunction which takes isPerfectNumber and intList as input and returns a List of Strings which contains the output if the number is perfect or not using map .

Code snippet:-

object ListMapHigherOrder{ def main(args :Array[String])

{

val intList :List[Int] = //write code to convert args to list of integers

def isPerfectNumber //write code to find is a number is perfect or not

}

def myHigherOrderFunction //complete the higher order function

}

println(myHigherOrderFunction(isPerfectNumber, intList))

}

}

Compile the program using:

scalac ListMapHigherOrder.scala

Execute the program to print the outputs:

scala ListMapHigherOrder.scala 3 6 7

Output will be: List(false, true, false)

Hi,

Can anyone please help me with the above problem?

I think the reason no one responded is that this very much smells like a homework problem, and people don’t want to solve other people’s homework for them.

You will most likely have more luck if you focus on one particular issue you are having trouble with and write a question in your own words.

2 Likes

I have tried to solve the problem as below. Here, i am getting the output as-
List(false)List(true)List(false).
But, the desired output is- List(false,true,false) [where, the input list is- List(3,6,7)]

Can anyone please look into the below code and help me to get the desired output i.e. List(false,true,false)?-

Object ListMapHigherOrder{

def main(args:Array[String])

{

val a=args(0).toInt

val b=args(1).toInt

val c=args(2).toInt

val intList:List[Int]=List(a,b,c)

def isPerfectNumber(num:Int):Boolean={

def perfect(n:Int):Boolean={

if(n<=0)

false

else

{

If((for(i<-2 to n/2 if n%i==0)yield i).sum+1==n)

true

else

false

}

}

perfect(num)

}

def myHigherOrderFunction(perfect: (Int)=>Boolean,intList:List[Int])={

val a:List[Boolean]=List();

intList.foreach((x:Int)=>print(List.concat(a,List(perfect(x)))))

}

myHigherOrderFunction(isPerfectNumber,intList)

}

}

Can someone please assist me to get the output in desired format i.e. List(false,true,false) for input list List(3,6,7)?
In the above code, i am getting the output as- List(false)List(true)List(false).

You have a list: List[Int], and you need a List[Boolean], and you have a function isPerfectNumber: (Int) => Boolean… I think you should be able to put this together: “list.map(isPerfectNumber)”.

Brian Maso

1 Like

object ListMapHigherOrder{
def main(args:Array[String])
{
//val input = Array(args(0).toString, args(1).toString, args(2).toString)
//val spString= ipstring.split(" ").toList
//val intList: List[Int] = spString.map(_.toString.toInt)

//working one
val intRes = args.toList
val intList: List[Int] = intRes.map(_.toInt).toList

//val ipstring = args(0).toString
//val spString= ipstring.split(" ").toList
//val intList = spString.map(_.toString.toInt)

def isPerfectNumber(ipnumber:Int):String={
var sum: Int = 0
for (i <- 1 to (ipnumber -1)){
val rem = ipnumber % i
if (rem == 0){sum = sum + i}
}
if (sum == ipnumber) {
//println(“Entered Number is perfect number”)
val res: String = “true”
return res}
else {
val res: String = “false”
return res}
}

def myHigherOrderFunction(argFn: Int => String, argVal:List[Int]): List[String]={
val res = argVal.map(argFn)
return res
}
println(myHigherOrderFunction(isPerfectNumber, intList))
}
}

1 Like

//val perfect = intList.map(isPerfectNumber)
output as List(false,true,false) but
not using map function

object ListMapHigherOrder{
def main(args :Array[String]){

val intList :List[Int] = args.map(_.toInt).toList

def isPerfectNumber(n: Int) :String = {
if ( (for (x <- 2 to n/2 if n % x == 0) yield x).sum + 1 == n )
return “true”
else
return “false”
}

def myHigherOrderFunction(f: (Int) => String, argList :List[Int]) :List[String] = {
return argList.map(f)

}

println(myHigherOrderFunction(isPerfectNumber, intList))
}
}

This is also throwing error… As per my understanding, it has been asked to avoid loop and use map.

if ( (for (x <- 2 to n/2 if n % x == 0) yield x).sum + 1 == n )

Can anyone help write the same program but instead of for loop use map ?
Thanks in Advance,
saraswat

I think you need to use a fold. If the collection isn’t too large, use a collect first. To make things like this understandable, I usually create a number of one-statement methods. That will (hopefully) make the operation understandable.

At that point, you can recombine the code and/or leave parts of it in the methods. The only reason to break it into methods is to aid in understanding the code. Once that’s transparent, reconstructing it is easy.

And shoot the original programmer. When I am the original programmer, I just hit my head against the desk for a minute. It helps remind me not to write this kind of thing in the future.

Any suggestion how it can be used in below for loop logic ?

object ListMapHigherOrder{
def main(args :Array[String]){

val intList :List[Int] = args.map(_.toInt).toList

def isPerfectNumber(n: Int) :String = {
if ( (for (x <- 2 to n/2 if n % x == 0) yield x).sum + 1 == n ) // can you please provide the logic
return “true”
else
return “false”
}

def myHigherOrderFunction(f: (Int) => String, argList :List[Int]) :List[String] = {
return argList.map(f)

}

println(myHigherOrderFunction(isPerfectNumber, intList))
}
}

Hi All,

Is there any suggesstion on this ?

what is your question, exactly, at this point? what aspect of this are you stuck on?

Here is the Problem Statement :

ListMapHigherOrder takes an array of string inputs args from the command line. Convert the array of strings args to intList, a list of integers. Write a function isPerfectNumber which takes integer input and returns String output. It finds if a number is perfect, and returns true if perfect, else returns false. A perfect number is a number whose sum factors (except for the number itself) is itself (example, 6 = 1+2+3). Write a higher order function myHigherOrderFunction which takes isPerfectNumber and intList as input, and returns a List of Strings which contain the output if the number is perfect or not using map.

Output will be: List(false, true, false)

Here is the Code snipped :

object ListMapHigherOrder{
def main(args :Array[String]){

val intList :List[Int] = args.map(_.toInt).toList

def isPerfectNumber(n: Int) :String = {
if ( (for (x <- 2 to n/2 if n % x == 0) yield x).sum + 1 == n ) // can you please provide the logic
return “true”
else
return “false”
}

def myHigherOrderFunction(f: (Int) => String, argList :List[Int]) :List[String] = {
return argList.map(f)

}

println(myHigherOrderFunction(isPerfectNumber, intList))
}
}

I am getting correct output using this logic. But in the problem statement is asking to write MAP function to get perfect number. In this above logic, I assume the below part is considered as not completed because it is being asked to use MAP function to do this for loop and condition check logic.

if ( (for (x <- 2 to n/2 if n % x == 0) yield x).sum + 1 == n )

can you please let me know how using MAP we can do the same task ? ( for loop , then condition check and summation)

The for loop here is just searching for factors of your target number. You could if desired build the numbers 2..n/2 as a list and filter it (which is a higherOrderedFunction) but without getting silly the for loop is your quickest way to solve what it is solving (i.e, finding factors, summing + 1, seeing if it matches your input and thus your input is a perfect number).

The problem statement is asking you to use map on your List[Int] with your isPerfectNumber function. Not to use map in isPerfectNumber.

Any issue with below code

object ListMapHigherOrder{
def main(args:Array[String])
{

val intRes = args.toList
val intList: List[Int] = intRes.map(_.toInt).toList

 def isPerfectNumber(input: Int) :String  = 
 {
 var check_sum = ( (2 to math.sqrt(input).toInt).collect  { case x if input % x == 0 => x + input / x}  ).sum 
 if ( check_sum == input - 1 )
      return "true"
      else 
      return "false"
      }

def myHigherOrderFunction(argFn: Int => String, argVal:List[Int]): List[String] = { argVal.map(argFn) }

println(myHigherOrderFunction(isPerfectNumber, intList))

}
}

Please let us know what issue with

object ListMapHigherOrder{
def main(args:Array[String])
{

val intRes = args.toList
val intList: List[Int] = intRes.map(_.toInt).toList

def isPerfectNumber(input: Int) :String =
{
var check_sum = ( (2 to math.sqrt(input).toInt).collect { case x if input % x == 0 => x + input / x} ).sum
if ( check_sum == input - 1 )
return “true”
else
return “false”
}

def myHigherOrderFunction(argFn: Int => String, argVal:List[Int]): List[String] = { argVal.map(argFn) }

println(myHigherOrderFunction(isPerfectNumber, intList))

}
}

getting required result but cannot pass exam

how to wrote code without var n return… seems after that code will work

Try changing var to val and simply omitting return (it’s rarely needed in Scala, if ever). Does anything go wrong? If so, what?

As an aside, Boolean would be a better than String as a return type for isPerfectNumber.