logo

Contare i numeri di un dato intervallo avente esattamente 5 fattori distinti

Dati due numeri interi l E R , l'attività è calcolare il conteggio dei numeri dall'intervallo [Sinistra, Destra] avendo esattamente 5 distinti fattori positivi.

Esempi:



Ingresso: L = 1, R = 100
Produzione: 2
Spiegazione: Gli unici due numeri nell'intervallo [1, 100] che hanno esattamente 5 fattori distinti sono 16 e 81.
I divisori di 16 sono {1, 2, 4, 8, 16}.
I fattori di 81 sono {1, 3, 9, 27, 81}.

sindacato contro sindacato tutto

Ingresso: L = 1, R = 100
Produzione: 2

Approccio ingenuo: L'approccio più semplice per risolvere questo problema è attraversare l'intervallo [Sinistra, Destra] e per ogni numero conta i suoi fattori. Se il conteggio dei fattori è uguale a 5 , incrementa il conteggio di 1 .
Complessità temporale: (R – L) × ?N
Spazio ausiliario: O(1)
Approccio efficiente: Per ottimizzare l'approccio di cui sopra, è necessario fare la seguente osservazione riguardo ai numeri aventi esattamente 5 fattori.



Sia p la scomposizione in fattori primi di un numero1UN1×p2UN2× … ×pNUNN.
Pertanto, il conteggio dei fattori di questo numero può essere scritto come (a1+1)×(a2+ 1)× … ×(aN+1).
Poiché questo prodotto deve essere uguale a 5 (il quale è un numero primo ), nel prodotto deve esistere solo un termine maggiore di 1. Quel termine deve essere uguale a 5.
Pertanto, se aio+1 = 5
=> aio= 4

Seguire i passaggi seguenti per risolvere il problema:

  • Il conteggio richiesto è il conteggio dei numeri nell'intervallo contenente p4come fattore, dove P è un numero primo.
  • Per calcolare in modo efficiente p4per una vasta gamma ( [1, 1018] ), l'idea è di utilizzare il Setaccio di Eratostene per memorizzare tutti i numeri primi fino a 104.5.

Di seguito è riportata l’implementazione dell’approccio di cui sopra:



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>>primo;> // 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 && pag<= R) { Count++; } } cout << Count << endl; } // Driver Code int main() { long long L = 16, R = 85000; Sieve(); countNumbers(L, R); return 0; }>

>

>

Giava




// 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 && pag<= 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#


lunghezza dell'array java



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

tutorial sull'oscillazione di java
>

>

Produzione:

7>

Complessità temporale: O(N * log(log(N)) )
Spazio ausiliario: SU)