Given an integer N, count the numbers having an odd number of factors from 1 to N (inclusive).
Example 1:
Input:
N = 5
Output:
2
Explanation:
From 1 - 5 only 2 numbers,
1 and 4 are having odd number
of factors.using namespace std;
int main()
{
int N=5, res = 0, div=0;
for (int i = 1; i <= N; ++i)
{
div = 0;
for (int j = 1; j <= i; ++j)
{
if (i % j == 0) div++;
}
if (div % 2) res++;
}
cout <<"\n\tNo. of factors = "<<res;
return 0;
}
Comments