Tasks Details
easy
1.
CountFactors
Count factors of given number n.
Task Score
100%
Correctness
100%
Performance
100%
A positive integer D is a factor of a positive integer N if there exists an integer M such that N = D * M.
For example, 6 is a factor of 24, because M = 4 satisfies the above condition (24 = 6 * 4).
Write a function:
class Solution { public int solution(int N); }
that, given a positive integer N, returns the number of its factors.
For example, given N = 24, the function should return 8, because 24 has 8 factors, namely 1, 2, 3, 4, 6, 8, 12, 24. There are no other factors of 24.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..2,147,483,647].
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C#
Time spent on task 2 minutes
Notes
not defined yet
Task timeline
Code: 20:41:44 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int N) {
var counter = 0;
var half = n/2;
if (n == 0) return -1;
if (n == 1) return 1;
counter = 2; //1 and itself
int sqrtPoint = (Int32)(Math.Truncate(Math.Sqrt(n)));
for (int i = 2; i <= sqrtPoint; i++)
{
if (n % i == 0)
{
counter += 2; // We found a pair of factors.
Console.WriteLine(n / i);
Console.WriteLine(i);
}
}
// Check if our number is an exact square.
if (sqrtPoint * sqrtPoint == n)
{
counter -=1;
}
return counter;
}
}
Analysis
Compile error
Compilation failed: 7 error(s), 0 warnings Solution.cs(11,18): error CS0103: The name `n' does not exist in the current context Solution.cs(12,11): error CS0103: The name `n' does not exist in the current context Solution.cs(13,11): error CS0103: The name `n' does not exist in the current context Solution.cs(15,55): error CS0103: The name `n' does not exist in the current context Solution.cs(18,13): error CS0103: The name `n' does not exist in the current context Solution.cs(21,29): error CS0103: The name `n' does not exist in the current context Solution.cs(26,36): error CS0103: The name `n' does not exist in the current context
Code: 20:41:50 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int n) {
var counter = 0;
var half = n/2;
if (n == 0) return -1;
if (n == 1) return 1;
counter = 2; //1 and itself
int sqrtPoint = (Int32)(Math.Truncate(Math.Sqrt(n)));
for (int i = 2; i <= sqrtPoint; i++)
{
if (n % i == 0)
{
counter += 2; // We found a pair of factors.
Console.WriteLine(n / i);
Console.WriteLine(i);
}
}
// Check if our number is an exact square.
if (sqrtPoint * sqrtPoint == n)
{
counter -=1;
}
return counter;
}
}
Analysis
Code: 20:41:59 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int n) {
var counter = 0;
var half = n/2;
if (n == 0) return -1;
if (n == 1) return 1;
counter = 2; //1 and itself
int sqrtPoint = (Int32)(Math.Truncate(Math.Sqrt(n)));
for (int i = 2; i <= sqrtPoint; i++)
{
if (n % i == 0)
{
counter += 2; // We found a pair of factors.
}
}
// Check if our number is an exact square.
if (sqrtPoint * sqrtPoint == n)
{
counter -=1;
}
return counter;
}
}
Analysis
Code: 20:42:05 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int n) {
var counter = 0;
var half = n/2;
if (n == 0) return -1;
if (n == 1) return 1;
counter = 2; //1 and itself
int sqrtPoint = (Int32)(Math.Truncate(Math.Sqrt(n)));
for (int i = 2; i <= sqrtPoint; i++)
{
if (n % i == 0)
{
counter += 2; // We found a pair of factors.
}
}
// Check if our number is an exact square.
if (sqrtPoint * sqrtPoint == n)
{
counter -=1;
}
return counter;
}
}
Analysis
Code: 20:42:07 UTC,
cs,
final,
score: 
100
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int n) {
var counter = 0;
var half = n/2;
if (n == 0) return -1;
if (n == 1) return 1;
counter = 2; //1 and itself
int sqrtPoint = (Int32)(Math.Truncate(Math.Sqrt(n)));
for (int i = 2; i <= sqrtPoint; i++)
{
if (n % i == 0)
{
counter += 2; // We found a pair of factors.
}
}
// Check if our number is an exact square.
if (sqrtPoint * sqrtPoint == n)
{
counter -=1;
}
return counter;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(sqrt(N))
expand all
Correctness tests
1.
0.059 s
OK
2.
0.059 s
OK
1.
0.059 s
OK
2.
0.059 s
OK
3.
0.059 s
OK
4.
0.059 s
OK
5.
0.060 s
OK
6.
0.060 s
OK
7.
0.059 s
OK
8.
0.060 s
OK
9.
0.059 s
OK
10.
0.060 s
OK
1.
0.060 s
OK
2.
0.059 s
OK
1.
0.060 s
OK
2.
0.060 s
OK
3.
0.063 s
OK
1.
0.060 s
OK
2.
0.059 s
OK
1.
0.059 s
OK
2.
0.059 s
OK
1.
0.060 s
OK
2.
0.060 s
OK
1.
0.060 s
OK
expand all
Performance tests
1.
0.060 s
OK
2.
0.060 s
OK
1.
0.059 s
OK
2.
0.059 s
OK
3.
0.059 s
OK
1.
0.060 s
OK
2.
0.060 s
OK
3.
0.059 s
OK
1.
0.059 s
OK
2.
0.059 s
OK
1.
0.059 s
OK
2.
0.060 s
OK
3.
0.059 s
OK
1.
0.059 s
OK
2.
0.060 s
OK
3.
0.059 s
OK