can someone help me solve the following #F question Recall our discussion of inf
ID: 3600055 • Letter: C
Question
can someone help me solve the following #F question
Recall our discussion of infinite streams in F#, with definition
type 'a stream = Cons of 'a * (unit -> 'a stream)
Use F# stream to generate an infinite stream of Armstrong Numbers defined as below:
A positive integer is an Armstrong number if it is equal to the sum of the cubes of its digits – e.g., the first such number is 1 and the second is 153.
write a program to output 2nd, 3rd, and 4th Armstrong numbers from the infinite stream. [NOTE: Armstrong Numbers are also defined in a different way – For a k-digit number, the sum of the kth power of its individual digits equals the number. Do not use that definition for this assignment.]
Explanation / Answer
Answer: See the code below
------------------------------------------
// Program to print Armstrong numbers
open System
let printArmstrongNumbers () =
let mutable num = 0 //number to be checked
let mutable countArmstrong = 1 //count Armstrong numbers
let mutable digit = 0
//loop to generate Armstrong numbers
while (countArmstrong <= 4) do
let mutable sumDigits = 0
num<-num+1
printfn "number being checked:%i" num
//check for being Armstrong number
let mutable temp = num
while (temp <>0) do
digit<-temp%10
sumDigits <- sumDigits+digit*digit*digit
temp<-temp/10
if (sumDigits = num) then
printfn "Armstrong number:%i" num
countArmstrong<-countArmstrong+1
let main() =
printArmstrongNumbers()
main()
-------------------------------------------------------------
Output:
--------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.