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


#1

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)


#2

Hi,

Can anyone please help me with the above problem?


#3

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.


#4

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)

}

}


#5

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).


#6

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


#7

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))
}
}


#8

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