|
-
February 6th, 2011, 06:45 AM
#1
Problem with get factored ?
Hey ,
I have this task but I don't know what I should do , Please help me.
task :
-------------
An integer that is greater than zero is said to be n-factored if it has n distinct factors other than 1 and itself. For example 12 is 4-factored because its factors are 2, 3, 4, 6. A prime number is 0-factored.
Write a function named isNFactored that takes two arguments, an integer and some value of n, and returns 1 if the integer is n-factored. The function signature is
int isNFactored(int value, int n)
Code:
if value n return reason
14 2 1 14 has two factors, 2 and 7
18 5 0 18 does not have 5 factors, it only has four (2, 3, 6 and 9)
25 2 0 25 = 5*5 but we are only interested in distinct factors so the 5 is only counted once.
36 7 1 36 has seven factors (2, 3, 4, 6, 9, 12, 18). Note that 6 is counted only once.
7 0 1 all prime numbers are 0-factored
-5 anything 0 the value must be greated than 0
and I do code know but I doesn't know what is meant by return type?
public static int GetFactorCount(int numberToCheck, int n)
{
int factorCount = 0;
int sqrt = n;
// Start from 1 as we want our method to also work when numberToCheck is 0 or 1.
for (int i = 1; i < sqrt; i++)
{
if (numberToCheck % i == 0)
{
factorCount += 2; // We found a pair of factors.
}
}
// Check if our number is an exact square.
if (sqrt * sqrt == numberToCheck)
{
factorCount++;
}
return factorCount;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|