Gegeven twee gehele getallen L En R , de taak is om het aantal getallen uit het bereik te berekenen [L, R] precies hebben 5 verschillende positieve factoren.
Voorbeelden:
Invoer: L = 1, R = 100
Uitgang: 2
Uitleg: De enige twee getallen in het bereik [1, 100] met precies vijf verschillende factoren zijn 16 en 81.
Delers van 16 zijn {1, 2, 4, 8, 16}.
Factoren van 81 zijn {1, 3, 9, 27, 81}.np.waarInvoer: L=1, R= 100
Uitgang: 2
Naïeve aanpak: De eenvoudigste manier om dit probleem op te lossen is door het bereik te doorkruisen [L, R] en tel voor elk getal de factoren ervan. Als het aantal factoren gelijk is aan 5 , verhoog het aantal met 1 .
Tijdcomplexiteit: (R – L) × ?N
Hulpruimte: O(1)
Efficiënte aanpak: Om de bovenstaande aanpak te optimaliseren, moet de volgende observatie worden gemaakt met betrekking tot getallen met precies vijf factoren.
Laat de priemfactorisatie van een getal p zijn1A1×p2A2× … ×pNAN.
Daarom kan het aantal factoren van dit getal worden geschreven als (a1+ 1)×(een2+ 1)× … ×(aN+ 1).
Omdat dit product gelijk moet zijn aan 5 (wat een is priemgetal ), mag er slechts één term groter dan 1 in het product voorkomen. Deze term moet gelijk zijn aan 5.
Daarom, als eeni+ 1 = 5
=> eeni= 4
Volg de onderstaande stappen om het probleem op te lossen:
- Het vereiste aantal is het aantal getallen in het bereik dat p bevat4als factor, waar P is een priemgetal.
- Voor het efficiënt berekenen van p4voor een groot bereik ( [1, 1018] ), is het de bedoeling om Sieve of Eratosthenes te gebruiken om alle priemgetallen tot en met op te slaan 104.5.
Hieronder vindt u de implementatie van de bovenstaande aanpak:
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 && 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.> |
mergesort-algoritme
>
>
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);> |
>
>Uitgang:
maak een sh-script uitvoerbaar
7>
Tijdcomplexiteit: O(N * log(log(N)) )
Hulpruimte: OP)