Java Program for Number of elements with odd factors in given range
Last Updated :
05 Dec, 2018
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.
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)>
Time Complexity: O(1)
For Math.pow() refer :
https://www.geeksforgeeks.org/java-lang-math-class-java-set-2/
Please refer complete article on
Number of elements with odd factors in given range for more details!
Similar Reads
Java Program to Find sum of even factors of a number Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, ⦠pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respect
3 min read
Java program for Find sum of odd factors of a number Write a java program for a given number n, the task is to find the odd factor sum. Examples: Input: n = 30Output: 24Explanation: Odd dividers sum 1 + 3 + 5 + 15 = 24 Input: 18Output: 13Explanation: Odd dividers sum 1 + 3 + 9 = 13 Java program for Find sum of odd factors of a number using Prime facto
3 min read
Java Program to Find the Number Occurring Odd Number of Times Write a Python program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space. Examples : Input: arr = {1, 2, 3, 2, 3, 1, 3}Output : 3 Input: arr = {5, 7, 2, 7, 5, 2, 5
4 min read
Java Program to Find the Frequency of Odd & Even Numbers in the Matrix Matrix is simply a two-dimensional array. Just like in one-dimensional traversal operation is required to figure out. The task is to find the frequencies of odd and even number in the given matrix. The size of matrix is defined as product of numbers of columns and rows. So, the size of the matrix is
3 min read
Java Program for Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 1
3 min read
Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve
3 min read