Given an integer n, count the numbers having an odd number of factors from 1 to n (inclusive).
Examples :
Input: n = 5
Output: 2
Explanation: The numbers from 1 to 5 with an odd number of factors are 1 and 4.
1 has 1 factor: [1]
4 has 3 factors: [1, 2, 4]
Hence, the count is 2.
Input: n = 1
Output: 1
Explanation: 1 has exactly one factor, [1], which is odd. Therefore, the count is 1
Table of Content
[Naive Approach] Check Factors for Every Number - O(n√n) Time O(1) Space
The idea is to iterate through all numbers from
1tonand count their factors by checking divisors up to their square root. If a number has an odd number of factors, increment the result. Finally, return the total count of such numbers.
#include <iostream>
using namespace std;
int count(int n)
{
int res = 0;
// Check every number from 1 to n
for (int i = 1; i <= n; i++)
{
int factors = 0;
// Count factors of i
for (int j = 1; j * j <= i; j++)
{
if (i % j == 0)
{
// Count the factor j
factors++;
// Count the paired factor if distinct
if (j != i / j)
{
factors++;
}
}
}
// If number of factors is odd, increment result
if (factors % 2 == 1)
{
res++;
}
}
return res;
}
// Driver Code
int main()
{
int n = 5;
cout << count(n);
return 0;
}
import java.io.*;
public class GfG {
public static int count(int n)
{
int res = 0;
// Check every number from 1 to n
for (int i = 1; i <= n; i++) {
int factors = 0;
// Count factors of i
for (int j = 1; j * j <= i; j++) {
if (i % j == 0) {
// Count the factor j
factors++;
// Count the paired factor if distinct
if (j != i / j) {
factors++;
}
}
}
// If number of factors is odd, increment result
if (factors % 2 == 1) {
res++;
}
}
return res;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
System.out.println(count(n));
}
}
def count(n):
res = 0
# Check every number from 1 to n
for i in range(1, n + 1):
factors = 0
# Count factors of i
for j in range(1, int(i ** 0.5) + 1):
if i % j == 0:
# Count the factor j
factors += 1
# Count the paired factor if distinct
if j != i // j:
factors += 1
# If number of factors is odd, increment result
if factors % 2 == 1:
res += 1
return res
# Driver Code
if __name__ == "__main__":
n = 5
print(count(n))
using System;
public class GfG {
public static int count(int n)
{
int res = 0;
// Check every number from 1 to n
for (int i = 1; i <= n; i++) {
int factors = 0;
// Count factors of i
for (int j = 1; j * j <= i; j++) {
if (i % j == 0) {
// Count the factor j
factors++;
// Count the paired factor if distinct
if (j != i / j) {
factors++;
}
}
}
// If number of factors is odd, increment result
if (factors % 2 == 1) {
res++;
}
}
return res;
}
// Driver Code
public static void Main()
{
int n = 5;
Console.WriteLine(count(n));
}
}
function count(n)
{
let res = 0;
// Check every number from 1 to n
for (let i = 1; i <= n; i++) {
let factors = 0;
// Count factors of i
for (let j = 1; j * j <= i; j++) {
if (i % j === 0) {
// Count the factor j
factors++;
// Count the paired factor if distinct
if (j !== i / j) {
factors++;
}
}
}
// If number of factors is odd, increment result
if (factors % 2 === 1) {
res++;
}
}
return res;
}
// Driver Code
let n = 5;
console.log(count(n));
Output
2
Time Complexity: O(n√n)
Auxiliary Space: O(1)
[Expected Approach] Count Perfect Squares - O(1) Time O(1) Space
The idea is based on the observation that only perfect squares have an odd number of factors. Therefore, the required count is simply the number of perfect squares in the range
[1, n], which is equal to the integer part of√n.
Factors occur in pairs (d, n/d), so a non-perfect square has an even number of factors. A perfect square has one unpaired factor, √n, giving it an odd number of factors. Hence, only perfect squares have an odd number of factors, and the answer is ⌊√n⌋.
- Initialize
n = 5. - Compute
sqrt(5), which is approximately2.236. - Convert it to an integer:
res = 2. - This means there are
2perfect squares (1and4) in the range[1, 5].
#include <cmath>
#include <iostream>
using namespace std;
int count(int n)
{
// Count of perfect squares from 1 to n
int res = (int)sqrt(n);
return res;
}
// Driver Code
int main()
{
int n = 5;
cout << count(n);
return 0;
}
import java.lang.Math;
public class GfG {
// Count of perfect squares from 1 to n
public static int count(int n)
{
int res = (int)Math.sqrt(n);
return res;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
System.out.println(count(n));
}
}
import math
# Count of perfect squares from 1 to n
def count(n):
res = int(math.sqrt(n))
return res
# Driver Code
if __name__ == "__main__":
n = 5
print(count(n))
using System;
public class GfG {
// Function to count the number of set bits in the given
// number.
public int count(int n)
{
int res = (int)(Math.Sqrt(n));
return res;
}
// Driver Code
public static void Main()
{
int n = 5;
GfG obj = new GfG();
Console.WriteLine(obj.count(n));
}
}
// Count of perfect squares from 1 to n
function count(n)
{
var res = Math.floor(Math.sqrt(n));
return res;
}
// Driver Code
var n = 5;
console.log(count(n));
Output
2
Time Complexity: O(1)
Auxiliary Space: O(1)