Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Need help with logic part C++

Name: Anonymous 2007-03-03 20:11 ID:BL8afESO

I know I need to use a loop in this program but I have no idea what to use

problem
The proper divisors of an integer n are the positive integer divisors whose values are less than n. A positive integer is said to be a deficient, perfect, or abundant number if the sum of its proper divisors is, respectively, less than, equal to, or greater than the number. For example, 8 is deficient because its proper divisors are 1, 2, and 4, and 1 + 2 + 4 < 8; The integer 6 is perfect because its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6; The integer 12 is abundant because its proper divisors are 1, 2, 3, 4, and 6, and
1 + 2 + 3 + 4 + 6 > 12. Write a program that classifies an integer n as being deficient, perfect, or abundant, for the following values of n:

               n = 20 to 30 inclusive
               n = 490 to 500 inclusive
               n = 8120 to 8130 inclusive

Display your results in a tabular format similar to the following example:

                Number                                     classification

                       8                                            deficient
                       6                                            perfect
                      12                                        abundant

Name: Anonymous 2007-03-03 22:17 ID:BENfLWO2

Hi there, I did your homework.

module Main
        where

properDivisors n = [x|x<-[1..ceiling((fromInteger n)/2.0)], (rem n x) == 0]

data Classification = Deficient | Perfect | Abundant

classify x
                | d > x = Abundant
                | d < x = Deficient
                | otherwise = Perfect
                where d = sum $ properDivisors x

main =
        let (numDefi, numPerf, numAbun) = foldr getCounts (0,0,0) $ map classify $ concat [[20..30], [490..500], [8120..8130]]
                where getCounts x (d,p,a) = case x of { Deficient -> (d+1,p,a) ; Perfect -> (d,p+1,a) ; Abundant -> (d,p,a+1) } in
        do
                putStrLn "Classification: Number"
                putStrLn "----------------------"
                putStrLn $ "Deficient: " ++ show numDefi
                putStrLn $ "Perfect: " ++ show numPerf
                putStrLn $ "Abundant: " ++ show numAbun

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List