
-----------------------------------
evogre3n
Wed Oct 06, 2004 8:33 pm

Deficient, abundant, and perfect
-----------------------------------
Hey everyone, i just found this site. Seems real cool, i hope to part of the community.

Anyways, i dont actually take tech this semester, and I have had no classes on Turing, but I am in the programming club, so i have built some knowledge without any teaching.

We were assigned this problem today.

We need to write a program that identifies the input number as deficient, abundant, or perfect. 

Deficient = the sum all factors (not including the number) is lower than the number

ABundant = the sum all factors is higher than the number

Perfect = the sum of all factors is equal to number.

ei. 12 is abundant because 6+4+3+2+1 > 12
ei. 6 is perfect because 3+2+1 = 6
ei. 4 is deficient because 2+1 < 4

So, I have got this far with the code with one of my friends.



var number,a,b :int
put "Input number"
get number
for x: 2..number
a := number div x 
if a * x = number then 
put a
end if
end for



That is to find out the factors and only to show the factors that are WHOLE numbers, and not decimals.

Now i think i need to add all those factors that the program put out.

And i have NO IDEA how to do that, seeing as everything you see in that code is how limited my knowledge of Turing is, ive only been using it for a couple of days, so i dont know any command that can add all those factors together.

Any help is greatly appreciated.

Thanks

Nick.

-----------------------------------
Tony
Wed Oct 06, 2004 8:41 pm


-----------------------------------
first of all, you find factors using

var number : int
put "Input number"
get number
for x : 2 .. ceil (number / 2)
    if number mod x = 0 then
        put x
    end if
end for

to record them, use arrays... though you dont seem to need to do that, so just put sum += x after put x and you're set

-----------------------------------
cycro1234
Wed Oct 06, 2004 8:43 pm


-----------------------------------
Yo! I'm that one friend!!  :lol: Yeah we need help on this. Please any advice???

-----------------------------------
evogre3n
Wed Oct 06, 2004 8:43 pm


-----------------------------------
wow
thanks a lot

but, i want to know, what exactly do all those commands mean, like MOD, what does that do?

 :D

-----------------------------------
wtd
Wed Oct 06, 2004 8:47 pm


-----------------------------------
Create an accumulating variable, then add each factor to it as you go.

-----------------------------------
cycro1234
Wed Oct 06, 2004 8:47 pm


-----------------------------------
we just recently started turing...

-----------------------------------
evogre3n
Wed Oct 06, 2004 8:52 pm


-----------------------------------
WTD, can you elaborate plz? im sorry im a total n00b at this, and so im real sorry if im bothering you guys, but if you have some spare time please help out

-----------------------------------
wtd
Wed Oct 06, 2004 8:53 pm


-----------------------------------
wow
thanks a lot

but, i want to know, what exactly do all those commands mean, like MOD, what does that do?

"mod" is related to integer arithmetic.  In integer arithmetic, dividing one integer by another gives you another integer.

You'd expect 

3 / 2

To give you 1.5, but instead, it gives you 1.  This is because 2 goes into 3 exactly once.  To get the remainder, you use mod.

-----------------------------------
wtd
Wed Oct 06, 2004 8:55 pm


-----------------------------------
WTD, can you elaborate plz? im sorry im a total n00b at this, and so im real sorry if im bothering you guys, but if you have some spare time please help out

You already loop over all of the numbers that could possibly be factors of the given number.  You also identify those factors.  

So you know whether a number is a factor or not.  If it is, then just add it to a "sum" variable.

-----------------------------------
evogre3n
Wed Oct 06, 2004 8:55 pm


-----------------------------------
okay i understand that know, but can you elaborate on the overall code that tony gave ?

He is busy atm as i see

-----------------------------------
cycro1234
Wed Oct 06, 2004 8:59 pm


-----------------------------------
Earlier we added the code we are currently using. Is there ne way to incorporate that into the final program? Right now we need to find the sum of the factors produced and we can't figure it out.

-----------------------------------
wtd
Wed Oct 06, 2004 9:01 pm


-----------------------------------
for x : 2 .. ceil (number / 2)

Here we start a loop.  The lowest possible factor we'll check is 2, since 1 is a factor of everything.  The highest factor we'll check is the original number divided by 2.  The highest factor of a number is always going to be at most half of that number.

if number mod x = 0 then
   put x
end if

If we divide our original number by the current number in our loop, and the remainder is zero, then we've found a factor and we print it out.

-----------------------------------
evogre3n
Wed Oct 06, 2004 9:03 pm


-----------------------------------
AHHH

okay, but now, whats all this talk about "sum"

how do we go about this?

See the problem isnt getting the factors, we figured out a different way to get them (even though its a lot different from yours)

The problem is adding the factors together ?

-----------------------------------
wtd
Wed Oct 06, 2004 9:13 pm


-----------------------------------
I and others have explained this numerous times, but one last time...

You a variable outside of your loop.  You initialize it to zero.  Then, in your loop, anytime you find a factor, you add that factor to the variable you created outside of the loop.

Psuedocode:

number is 6.
sum is 0.

first time:
   possible factor = 1
   6 divided by 1 leaves a remander of 0.
   1 is a factor of 6.  
   add 1 to sum.
   sum is 1.
second time:
   possible factor = 2
   6 divided by 2 leaves a remander of 0.
   2 is a factor of 6.  
   add 2 to sum.
   sum is 3.
third time:
   possible factor = 3
   6 divided by 3 leaves a remander of 0.
   3 is a factor of 6.  
   add 3 to sum.
   sum is 6.
fourth time:
   possible factor = 4
   6 divided by 4 leaves a remander of 2.
   4 is not a factor of 6. 
   sum is 6.
fifth time:
   possible factor = 5
   6 divided by 5 leaves a remander of 1.
   5 is not a factor of 6. 
   sum is 6.

-----------------------------------
cycro1234
Wed Oct 06, 2004 9:16 pm


-----------------------------------
Aight guys, thx for all your help! We'll take it from here..however slowly it maybe. Thx again

-----------------------------------
evogre3n
Thu Oct 07, 2004 6:38 am


-----------------------------------
Okay, thanks for all your help guys!



var number,sum : int
sum := 0
put "Input number"
get number 
for x: 2..ceil (number / 2)
    if number mod x = 0 then 
        sum += x
    end if 
end for
put " "
put "Here is the sum of all the factors not including 1 or the number"
put sum + 1
if sum + 1 > number then
put "The number is abundant"
elsif sum + 1 < number then
put "The number is deficient"
else sum := number
put "The number is perfect"
end if



Thats the final code. thanks a lot :D
