Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Deficient, abundant, and perfect
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
evogre3n




PostPosted: Wed Oct 06, 2004 8:33 pm   Post subject: 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.

code:


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.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Oct 06, 2004 8:41 pm   Post subject: (No subject)

first of all, you find factors using
code:

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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
cycro1234




PostPosted: Wed Oct 06, 2004 8:43 pm   Post subject: (No subject)

Yo! I'm that one friend!! Laughing Yeah we need help on this. Please any advice???
evogre3n




PostPosted: Wed Oct 06, 2004 8:43 pm   Post subject: (No subject)

wow
thanks a lot

but, i want to know, what exactly do all those commands mean, like MOD, what does that do?

Very Happy
wtd




PostPosted: Wed Oct 06, 2004 8:47 pm   Post subject: (No subject)

Create an accumulating variable, then add each factor to it as you go.
cycro1234




PostPosted: Wed Oct 06, 2004 8:47 pm   Post subject: (No subject)

we just recently started turing...
evogre3n




PostPosted: Wed Oct 06, 2004 8:52 pm   Post subject: (No subject)

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




PostPosted: Wed Oct 06, 2004 8:53 pm   Post subject: (No subject)

evogre3n wrote:
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

code:
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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Oct 06, 2004 8:55 pm   Post subject: (No subject)

evogre3n wrote:
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




PostPosted: Wed Oct 06, 2004 8:55 pm   Post subject: (No subject)

okay i understand that know, but can you elaborate on the overall code that tony gave ?

He is busy atm as i see
cycro1234




PostPosted: Wed Oct 06, 2004 8:59 pm   Post subject: (No subject)

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




PostPosted: Wed Oct 06, 2004 9:01 pm   Post subject: (No subject)

code:
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.

code:
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




PostPosted: Wed Oct 06, 2004 9:03 pm   Post subject: (No subject)

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




PostPosted: Wed Oct 06, 2004 9:13 pm   Post subject: (No subject)

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:

code:
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




PostPosted: Wed Oct 06, 2004 9:16 pm   Post subject: (No subject)

Aight guys, thx for all your help! We'll take it from here..however slowly it maybe. Thx again
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: