Number of elements with odd factors in given range
Last Updated :
25 Jun, 2022
Given a range [n,m], find the number of elements that have odd number of factors in the given range (n and m inclusive).
Examples :
Input : n = 5, m = 100
Output : 8
The numbers with odd factors are 9, 16, 25,
36, 49, 64, 81 and 100
Input : n = 8, m = 65
Output : 6
Input : n = 10, m = 23500
Output : 150
A Simple Solution is to loop through all numbers starting from n. For every number, check if it has an even number of factors. If it has an even number of factors then increment count of such numbers and finally print the number of such elements. To find all divisors of a natural number efficiently, refer All divisors of a natural number
An Efficient Solution is to observe the pattern. Only those numbers, which are perfect Squares have an odd number of factors. Let us analyze this pattern through an example.
For example, 9 has odd number of factors, 1, 3 and 9. 16 also has odd number of factors, 1, 2, 4, 8, 16. The reason for this is, for numbers other than perfect squares, all factors are in the form of pairs, but for perfect squares, one factor is single and makes the total as odd.
How to find number of perfect squares in a range?
The answer is difference between square root of m and n-1 (not n)
There is a little caveat. As both n and m are inclusive, if n is a perfect square, we will get an answer which is less than one the actual answer. To understand this, consider range [4, 36]. Answer is 5 i.e., numbers 4, 9, 16, 25 and 36.
But if we do (36**0.5) - (4**0.5) we get 4. So to avoid this semantic error, we take n-1.
C++
// C++ program to count number of odd squares
// in given range [n, m]
#include <bits/stdc++.h>
using namespace std;
int countOddSquares(int n, int m)
{
return (int)pow(m,0.5) - (int)pow(n-1,0.5);
}
// Driver code
int main()
{
int n = 5, m = 100;
cout << "Count is " << countOddSquares(n, m);
return 0;
}
Java
// Java program to count number of odd squares
// in given range [n, m]
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
public static int countOddSquares(int n, int m)
{
return (int)Math.pow((double)m,0.5) - (int)Math.pow((double)n-1,0.5);
}
// Driver code for above functions
public static void main (String[] args)
{
int n = 5, m = 100;
System.out.print("Count is " + countOddSquares(n, m));
}
}
// Mohit Gupta_OMG <(o_0)>
Python3
# Python program to count number of odd squares
# in given range [n, m]
def countOddSquares(n, m):
return int(m**0.5) - int((n-1)**0.5)
# Driver code
n = 5
m = 100
print("Count is", countOddSquares(n, m))
# Mohit Gupta_OMG <0_o>
C#
// C# program to count number of odd
// squares in given range [n, m]
using System;
class GFG {
// Function to count odd squares
public static int countOddSquares(int n, int m)
{
return (int)Math.Pow((double)m, 0.5) -
(int)Math.Pow((double)n - 1, 0.5);
}
// Driver code
public static void Main ()
{
int n = 5, m = 100;
Console.Write("Count is " + countOddSquares(n, m));
}
}
// This code is contributed by Nitin Mittal.
PHP
<?php
// PHP program to count
// number of odd squares
// in given range [n, m]
function countOddSquares($n, $m)
{
return pow($m, 0.5) -
pow($n - 1, 0.5);
}
// Driver code
$n = 5; $m = 100;
echo "Count is " ,
countOddSquares($n, $m);
// This code is contributed
// by nitin mittal.
?>
JavaScript
<script>
// JavaScript program to count number of odd squares
// in given range [n, m]
function countOddSquares(n, m)
{
return Math.pow(m,0.5) - Math.pow(n-1,0.5);
}
// Driver Code
let n = 5, m = 100;
document.write("Count is " + countOddSquares(n, m));
</script>
Output :
Count is 8
Time Complexity : O(1)
Auxiliary Space: O(1)
Similar Reads
Number of elements with even factors in the given range Given a range [n, m], the task is to find the number of elements that have even number of factors in the given range (n and m inclusive).Examples : Input: n = 5, m = 20 Output: 14 The numbers with even factors are 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20. Input: n = 5, m = 100 Output: 88 A
3 min read
Sum of all even factors of numbers in the range [l, r] Given a range [l, r], the task is to find the sum of all the even factors of the numbers from the given range.Examples: Input: l = 6, r = 8 Output: 22 factors(6) = 1, 2, 3, 6, evenfactors(6) = 2, 6 sumEvenFactors(6) = 2 + 6 = 8 factors(7) = 1, 7, No even factors factors(8) = 1, 2, 4, 8, evenfactors(
15+ min read
Sum of all odd factors of numbers in the range [l, r] Given a range [l, r], the task is to find the sum of all the odd factors of the numbers from the given range.Examples: Input: l = 6, r = 8 Output: 32 factors(6) = 1, 2, 3, 6, oddfactors(6) = 1, 3 sum_Odd_Factors(6) = 1 + 3 = 4 factors(7) = 1, 7, oddfactors(6) = 1 7, sum_Odd_Factors(7) = 1 + 7 = 8 fa
6 min read
Find numbers with K odd divisors in a given range Given two numbers a and b, and a number k which is odd. The task is to find all the numbers between a and b (both inclusive) having exactly k divisors.Examples: Input : a = 2, b = 49, k = 3 Output: 4 // Between 2 and 49 there are four numbers // with three divisors // 4 (Divisors 1, 2, 4), 9 (Diviso
8 min read
k-th prime factor of a given number Given two numbers n and k, print k-th prime factor among all prime factors of n. For example, if the input number is 15 and k is 2, then output should be "5". And if the k is 3, then output should be "-1" (there are less than k prime factors). Examples: Input : n = 225, k = 2 Output : 3 Prime factor
15+ min read
Sort elements on the basis of number of factors Given an array of positive integers. Sort the given array in decreasing order of number of factors of each element, i.e., element having the highest number of factors should be the first to be displayed and the number having least number of factors should be the last one. Two elements with equal num
10 min read