Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmNumber of elements with even factors in the given range

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 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 find the numbers with odd number of factors i.e only the perfect squares have odd number of factors, so all numbers other than perfect squares will have even number of factors. So, find the count of perfect squares in the range and subtract from the total numbers i.e. m-n+1 .
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the perfect squares
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 "
         << (m - n + 1) - countOddSquares(n, m);
    return 0;
}


Java




// Java implementation of the above approach
import java.io.*;
 
class GFG
{
     
// Function to count the perfect squares
static int countOddSquares(int n, int m)
{
    return (int)Math.pow(m, 0.5) -
            (int)Math.pow(n - 1, 0.5);
}
 
// Driver code
public static void main (String[] args)
{
    int n = 5, m = 100;
    System.out.println("Count is " + ((m - n + 1)
                    - countOddSquares(n, m)));
}
}
 
// This code is contributed by ajit..


Python 3




# Python3 implementation of the
# above approach
 
# Function to count the perfect squares
def countOddSquares(n, m) :
    return (int(pow(m, 0.5)) -
            int(pow(n - 1, 0.5)))
 
# Driver code
if __name__ == "__main__" :
 
    n = 5 ; m = 100;
    print("Count is", (m - n + 1) -
                       countOddSquares(n, m))
     
# This code is contributed by Ryuga


C#




// C# implementation of the above approach
using System;
 
class GFG
{
         
// Function to count the perfect squares
static int countOddSquares(int n, int m)
{
    return (int)Math.Pow(m, 0.5) -
            (int)Math.Pow(n - 1, 0.5);
}
 
// Driver code
static public void Main ()
{
    int n = 5, m = 100;
    Console.WriteLine("Count is " + ((m - n + 1)
                    - countOddSquares(n, m)));
}
}
 
// This Code is contributed by akt_mit.


PHP




<?php
// PHP implementation of the
// above approach
 
// Function to count the perfect squares
function countOddSquares($n, $m)
{
    return (int)pow($m, 0.5) -
           (int)pow($n - 1, 0.5);
}
 
// Driver code
$n = 5;
$m = 100;
echo "Count is ", ($m - $n + 1) -
                   countOddSquares($n, $m);
     
// This code is contributed by ajit
?>


Javascript




<script>
 
// Javascript implementation of the above approach
 
// Function to count the perfect squares
function countOddSquares(n, m)
{
    return Math.pow(m,0.5) - Math.pow(n - 1, 0.5);
}
 
// Driver code
var n = 5, m = 100;
document.write( "Count is "
     + ((m - n + 1) - countOddSquares(n, m)));
 
</script>


Output: 

Count is 88

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments