Підрахуйте числа з заданого діапазону, що містить рівно 5 різних множників

Дано два цілих числа Л і Р , завдання обчислити кількість чисел із діапазону [L, R] маючи саме 5 яскравих позитивних факторів.

приклади:

введення: L = 1, R = 100
Вихід: 2
Пояснення: Єдиними двома числами в діапазоні [1, 100], які мають рівно 5 різних множників, є 16 і 81.
Дільники числа 16: {1, 2, 4, 8, 16}.
Дільники числа 81: {1, 3, 9, 27, 81}.

введення: L = 1, R = 100
Вихід: 2

Наївний підхід: Найпростішим підходом до вирішення цієї проблеми є проходження діапазону [L, R] і для кожного числа порахуйте його множники. Якщо кількість факторів дорівнює 5 , збільшити кількість на 1 .
Часова складність: (R – L) × ?N
Допоміжний простір: О(1)
Ефективний підхід: Щоб оптимізувати наведений вище підхід, необхідно зробити наступне спостереження щодо чисел, які мають рівно 5 факторів.

Нехай простий розклад числа дорівнює p 1 a 1 ×стор 2 a 2 × … ×стор п a п .
Тому кількість множників цього числа можна записати як (a 1 + 1)×(a 2 + 1)× … ×(a п + 1).
Оскільки цей добуток повинен дорівнювати 5 (що є а просте число ), лише один термін, більший за 1, повинен існувати в продукті. Цей член повинен дорівнювати 5.
Тому, якщо a i + 1 = 5
=> а i = 4

Щоб вирішити проблему, виконайте наведені нижче дії.

  • Необхідна кількість — це кількість чисел у діапазоні, що містить p 4 як фактор, де стор є простим числом.
  • Для ефективного розрахунку с 4 для великого діапазону ( [1, 10 18 ] ), ідея полягає в тому, щоб використовувати Решето Ератосфена для зберігання всіх простих чисел до 10 4.5 .

Нижче наведено реалізацію вищезазначеного підходу:

C++14




// C++ Program to implement> // the above approach> #include> using> namespace> std;> const> int> N = 2e5;> // Stores all prime numbers> // up to 2 * 10^5> vector <> long> long> >простий;> // Function to generate all prime> // numbers up to 2 * 10 ^ 5 using> // Sieve of Eratosthenes> void> Sieve()> {> > prime.clear();> > vector <> bool> >p(N + 1,> true> );> > // Mark 0 and 1 as non-prime> > p[0] = p[1] => false> ;> > for> (> int> i = 2; i * i <= N; i++) {> > // If i is prime> > if> (p[i] ==> true> ) {> > // Mark all its factors as non-prime> > for> (> int> j = i * i; j <= N; j += i) {> > p[j] => false> ;> > }> > }> > }> > for> (> int> i = 1; i // If current number is prime if (p[i]) { // Store the prime prime.push_back(1LL * pow(i, 4)); } } } // Function to count numbers in the // range [L, R] having exactly 5 factors void countNumbers(long long int L, long long int R) { // Stores the required count int Count = 0; for (int p : prime) { if (p>= L && p <= R) { Count++; } } cout < < Count < < endl; } // Driver Code int main() { long long L = 16, R = 85000; Sieve(); countNumbers(L, R); return 0; }>

Java




// Java Program to implement> // the above approach> import> java.util.*;> class> GFG> {> > static> int> N => 200000> ;> > // Stores all prime numbers> > // up to 2 * 10^5> > static> int> prime[] => new> int> [> 20000> ];> > static> int> index => 0> ;> > // Function to generate all prime> > // numbers up to 2 * 10 ^ 5 using> > // Sieve of Eratosthenes> > static> void> Sieve()> > {> > index => 0> ;> > int> p[] => new> int> [N +> 1> ];> > for> (> int> i => 0> ; i <= N; i++)> > {> > p[i] => 1> ;> > }> > // Mark 0 and 1 as non-prime> > p[> 0> ] = p[> 1> ] => 0> ;> > for> (> int> i => 2> ; i * i <= N; i++)> > {> > // If i is prime> > if> (p[i] ==> 1> )> > {> > // Mark all its factors as non-prime> > for> (> int> j = i * i; j <= N; j += i)> > {> > p[j] => 0> ;> > }> > }> > }> > for> (> int> i => 1> ; i { // If current number is prime if (p[i] == 1) { // Store the prime prime[index++] = (int)(Math.pow(i, 4)); } } } // Function to count numbers in the // range [L, R] having exactly 5 factors static void countNumbers(int L,int R) { // Stores the required count int Count = 0; for(int i = 0; i { int p = prime[i]; if (p>= L && p <= R) { Count++; } } System.out.println(Count); } // Driver Code public static void main(String[] args) { int L = 16, R = 85000; Sieve(); countNumbers(L, R); } } // This code is contributed by amreshkumar3.>

Python3




# Python3 implementation of> # the above approach> N> => 2> *> 100000> # Stores all prime numbers> # up to 2 * 10^5> prime> => [> 0> ]> *> N> # Function to generate all prime> # numbers up to 2 * 10 ^ 5 using> # Sieve of Eratosthenes> def> Sieve() :> > p> => [> True> ]> *> (N> +> 1> )> > # Mark 0 and 1 as non-prime> > p[> 0> ]> => p[> 1> ]> => False> > i> => 2> > while> (i> *> i <> => N) :> > # If i is prime> > if> (p[i]> => => True> ) :> > # Mark all its factors as non-prime> > for> j> in> range> (i> *> i, N, i):> > p[j]> => False> > i> +> => 1> > for> i> in> range> (N):> > # If current number is prime> > if> (p[i] !> => False> ) :> > # Store the prime> > prime.append(> pow> (i,> 4> ))> # Function to count numbers in the> # range [L, R] having exactly 5 factors> def> countNumbers(L, R) :> > # Stores the required count> > Count> => 0> > for> p> in> prime :> > if> (p>> => L> and> p <> => R) :> > Count> +> => 1> > print> (Count)> # Driver Code> L> => 16> R> => 85000> Sieve()> countNumbers(L, R)> # This code is contributed by code_hunt.>

C#




// C# Program to implement> // the above approach> using> System;> class> GFG> {> > static> int> N = 200000;> > // Stores all prime numbers> > // up to 2 * 10^5> > static> int> []prime => new> int> [20000];> > static> int> index = 0;> > // Function to generate all prime> > // numbers up to 2 * 10 ^ 5 using> > // Sieve of Eratosthenes> > static> void> Sieve()> > {> > index = 0;> > int> []p => new> int> [N + 1];> > for> (> int> i = 0; i <= N; i++)> > {> > p[i] = 1;> > }> > // Mark 0 and 1 as non-prime> > p[0] = p[1] = 0;> > for> (> int> i = 2; i * i <= N; i++)> > {> > // If i is prime> > if> (p[i] == 1)> > {> > // Mark all its factors as non-prime> > for> (> int> j = i * i; j <= N; j += i)> > {> > p[j] = 0;> > }> > }> > }> > for> (> int> i = 1; i { // If current number is prime if (p[i] == 1) { // Store the prime prime[index++] = (int)(Math.Pow(i, 4)); } } } // Function to count numbers in the // range [L, R] having exactly 5 factors static void countNumbers(int L,int R) { // Stores the required count int Count = 0; for(int i = 0; i { int p = prime[i]; if (p>= L && p <= R) { Count++; } } Console.WriteLine(Count); } // Driver Code public static void Main(String[] args) { int L = 16, R = 85000; Sieve(); countNumbers(L, R); } } // This code is contributed by shikhasingrajput>

Javascript




> // javascript program of the above approach> let N = 200000;> > > // Stores all prime numbers> > // up to 2 * 10^5> > let prime => new> Array(20000).fill(0);> > let index = 0;> > > // Function to generate all prime> > // numbers up to 2 * 10 ^ 5 using> > // Sieve of Eratosthenes> > function> Sieve()> > {> > index = 0;> > let p => new> Array (N + 1).fill(0);> > for> (let i = 0; i <= N; i++)> > {> > p[i] = 1;> > }> > > // Mark 0 and 1 as non-prime> > p[0] = p[1] = 0;> > for> (let i = 2; i * i <= N; i++)> > {> > > // If i is prime> > if> (p[i] == 1)> > {> > > // Mark all its factors as non-prime> > for> (let j = i * i; j <= N; j += i)> > {> > p[j] = 0;> > }> > }> > }> > for> (let i = 1; i { // If current number is prime if (p[i] == 1) { // Store the prime prime[index++] = (Math.pow(i, 4)); } } } // Function to count numbers in the // range [L, R] having exactly 5 factors function countNumbers(L, R) { // Stores the required count let Count = 0; for(let i = 0; i { let p = prime[i]; if (p>= L && p <= R) { Count++; } } document.write(Count); } // Driver Code let L = 16, R = 85000; Sieve(); countNumbers(L, R);>

Вихід:

7 

Часова складність: O(N * log(log(N)) )
Допоміжний простір: O(N)



Вам Може Сподобатися