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)



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.


I have tried to solve the problem as below. Here, i am getting the output as-
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((for(i<-2 to n/2 if n%i==0)yield i).sum+1==n)








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

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







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: “”.

Brian Maso