Đưa ra một phạm vi [n, m], tìm số lượng các yếu tố có số lượng các yếu tố lẻ trong phạm vi đã cho [bao gồm N và M].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
Một giải pháp đơn giản là lặp qua tất cả các số bắt đầu từ n. Đối với mỗi số, hãy kiểm tra xem nó có số lượng yếu tố chẵn không. Nếu nó có số lượng các yếu tố chẵn thì tăng số lượng của các số đó và cuối cùng in số lượng các yếu tố đó. Để tìm tất cả các ước số của một số tự nhiên một cách hiệu quả, hãy tham khảo tất cả các ước số của một số tự nhiênSimple 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
Một giải pháp hiệu quả là quan sát mô hình. Chỉ những con số đó, là hình vuông hoàn hảo mới có một số yếu tố lẻ. Hãy để chúng tôi phân tích mô hình này thông qua một ví dụ.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.
Ví dụ, 9 có số lượng các yếu tố lẻ, 1, 3 và 9. 16 cũng có số lượng các yếu tố lẻ, 1, 2, 4, 8, 16. Lý do cho điều này là, đối với các số khác với hình vuông hoàn hảo, tất cả các yếu tố là Ở dạng các cặp, nhưng đối với hình vuông hoàn hảo, một yếu tố là đơn và làm cho tổng số trở nên kỳ lạ.
Làm thế nào để tìm số lượng hình vuông hoàn hảo trong một phạm vi? Câu trả lời là sự khác biệt giữa căn bậc hai của m và n-1 [không phải n] có một chút cảnh báo. Vì cả N và M đều bao gồm, nếu N là một hình vuông hoàn hảo, chúng ta sẽ nhận được câu trả lời ít hơn một câu trả lời thực tế. Để hiểu điều này, hãy xem xét phạm vi [4, 36]. Trả lời là 5 tức là, số 4, 9, 16, 25 và 36. Nhưng nếu chúng ta làm [36 ** 0.5]-[4 ** 0.5] Chúng ta sẽ nhận được 4. Vì vậy, để tránh lỗi ngữ nghĩa này, chúng ta sẽ lấy N-1.
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.
def
countOddSquares[n, m]:
return
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 : 1504
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 : 1505
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 : 1506
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 : 1507
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 : 1505
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 : 1509
Count is 80
Count is 81
Count is 82
Count is 83
Output:
Count is 8
Độ phức tạp về thời gian: O [1]
Vui lòng tham khảo hoàn thành bài viết về số lượng các yếu tố với các yếu tố kỳ lạ trong phạm vi nhất định để biết thêm chi tiết!
Đặt P1, P2, PK PK là yếu tố chính của n. Đặt A1, A2, .. Ak là sức mạnh cao nhất của P1, P2, .. PK tương ứng chia n, tức là, chúng ta có thể viết n là n = [p1a1]* [p2a2]*, [pkak].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
Làm thế nào để bạn tìm thấy số lượng các yếu tố lẻ?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++
Count is 84
Count is 85
Count is 86
Count is 87
int
Count is 89
int
def
1int
def
3def
4
def
5return
Count is 81
int
__19____505051int
____________countOddSquares[n, m]:
0505555countOddSquares[n, m]:
6
int
countOddSquares[n, m]:
8
def
4
int
2
4
5
6
return
9
countOddSquares[n, m]:
6
Java
return
1 return
2
return
1 return
4
return
1 return
6
return
7 return
8
def
4
int
1 int
2 int
Count is 89
int
def
1int
def
3
def
4
Các
countOddSquares[n, m]:
6
int
1 int
2
Count is 813
Count is 814
def
4
[m
1int
Count is 819
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 : 1506
Count is 821
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 : 1509
Count is 823
[m
1
Count is 825
5 Count is 827
countOddSquares[n, m]:
6
countOddSquares[n, m]:
6
Python3
def
countOddSquares[n, m]:
return
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 : 1504
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 : 1505
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 : 1506
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 : 1507
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 : 1505
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 : 1509
Count is 80
Count is 81
Count is 82
Count is 83
C#
Count is 85
Count is 862
return
7
Count is 864
int
1 int
2 int
Count is 89
int
def
1int
def
3
def
4
Các
Count is 883
Count is 81
int
Count is 880
[m
6Count is 888
countOddSquares[n, m]:
6
int
1 int
2
Count is 813
Count is 814
def
4
[m
1int
Count is 819
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 : 1506
Count is 821
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 : 1509
Count is 823
[m
1
Count is 825
5 Count is 827
countOddSquares[n, m]:
6
countOddSquares[n, m]:
6
def
countOddSquares[n, m]:
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 : 15008
return
def
4
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 : 1504
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 : 1505
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 : 1506
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 : 1507
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 : 1505
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 : 1509
countOddSquares[n, m]:
6
Count is 85
Count is 862
return
7
Count is 864
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 : 15032
Count is 89
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 : 15011
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 : 15012
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 : 15013
Count is 807
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 : 15038
‘
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 : 15039
int
1 int
2
Count is 813
Count is 895
def
4
[m
1int
2
countOddSquares[n, m]:
6
[m
1
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 : 15050
[m
1
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 : 15002
5 Count is 827
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 : 15055
PHP
Count is 8
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
09 Count is 8
9Input : 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
11Input : 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
121213Count is 8
9 O[1]
Auxiliary Space: O[1]
return
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 : 15017
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 : 15013
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 : 15019Aarti_Rathi and Divyanshu Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.