Computer Science Canada factors of a number |
Author: | sweetiechic [ 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 ![]() p.s. i need to know how to find the number of factors, not the factors themselves! |
Author: | Cervantes [ Tue Oct 03, 2006 10:57 pm ] |
Post 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. |