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

Username:   Password: 
 RegisterRegister   
 factors of a number
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sweetiechic




PostPosted: Tue Oct 03, 2006 10:49 pm   Post subject: factors of a number

if you were asked to output the numbers of factors of..say 6, how would you do that? i already know the answer is 4 but i can only seem to list the factors 1,2,3 and 6 using counted loops..and then im kinda lost from there Confused
p.s. i need to know how to find the number of factors, not the factors themselves!
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Tue Oct 03, 2006 10:57 pm   Post subject: (No subject)

The easiest way to do this would be to count from 1 up to 6 (using a for loop) and then check each number in that range if 6 mod that number equals zero.

Then, realize that you don't need to count all the way up to 6. Knowing that 6 mod 1 = 0 means that 1 is a factor and knowing that 1 is a factor tells you that 6/1 = 6 is a factor. Knowing that 2 is a factor tells you that 6/2 = 3 is a factor. So you only need to count up to the squareroot of 6, rounded down.

There are faster ways to do prime factorization, but this is the easiest.
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 1  [ 2 Posts ]
Jump to:   


Style:  
Search: