Räkna siffror från ett givet intervall med exakt 5 distinkta faktorer

Givet två heltal L och R , uppgiften är att beräkna antalet siffror från intervallet [L, R] ha exakt 5 tydliga positiva faktorer.

Exempel:

Inmatning: L = 1, R = 100
Produktion: 2
Förklaring: De enda två talen i intervallet [1, 100] som har exakt 5 distinkta faktorer är 16 och 81.
Faktorer på 16 är {1, 2, 4, 8, 16}.
Faktorer på 81 är {1, 3, 9, 27, 81}.

Inmatning: L = 1, R = 100
Produktion: 2

Naivt förhållningssätt: Det enklaste sättet att lösa detta problem är att korsa räckvidden [L, R] och för varje nummer, räkna dess faktorer. Om antalet faktorer är lika med 5 , öka räkna med 1 .
Tidskomplexitet: (R – L) × ?N
Hjälputrymme: O(1)
Effektivt tillvägagångssätt: För att optimera ovanstående tillvägagångssätt måste följande observation göras angående tal som har exakt 5 faktorer.

Låt primtalsfaktoriseringen av ett tal vara p 1 a 1 × sid 2 a 2 × … × sid n a n .
Därför kan antalet faktorer av detta antal skrivas som (a 1 + 1)×(a 2 + 1)× … ×(a n + 1).
Eftersom denna produkt måste vara lika med 5 (Vilket är en primtal ), endast en term större än 1 måste finnas i produkten. Den termen måste vara lika med 5.
Därför, om a i + 1 = 5
=> a i = 4

Följ stegen nedan för att lösa problemet:

  • Det obligatoriska antalet är antalet siffror i intervallet som innehåller p 4 som en faktor, var sid är ett primtal.
  • För effektiv beräkning av sid 4 för ett stort utbud ( [1, 10 18 ] ), är tanken att använda Sieve of Eratosthenes för att lagra alla primtal upp till 10 4.5 .

Nedan är implementeringen av ovanstående tillvägagångssätt:

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> >prime;> // 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 && sid <= 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 && sid <= 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 && sid <= 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 && sid <= R) { Count++; } } document.write(Count); } // Driver Code let L = 16, R = 85000; Sieve(); countNumbers(L, R);>

Produktion:

7 

Tidskomplexitet: O(N * log(log(N)) )
Hjälputrymme: PÅ)