Laske numerot tietyltä alueelta, jolla on täsmälleen 5 erillistä tekijää
Annettu kaksi kokonaislukua L ja R , tehtävänä on laskea lukujen määrä alueella [L, R] joilla on täsmälleen 5 erillistä positiivista tekijää.
Esimerkkejä:
Syöte: L = 1, R = 100
Lähtö: 2
Selitys: Ainoat kaksi numeroa alueella [1, 100], joilla on täsmälleen 5 erillistä tekijää, ovat 16 ja 81.
Tekijät 16 ovat {1, 2, 4, 8, 16}.
Tekijät 81 ovat {1, 3, 9, 27, 81}.Syöte: L = 1, R = 100
Lähtö: 2
Naiivi lähestymistapa: Yksinkertaisin tapa ratkaista tämä ongelma on kulkea alueen läpi [L, R] ja laske jokaisen luvun tekijät. Jos tekijöiden määrä on yhtä suuri kuin 5 , lisää laskentaa 1 .
Aika monimutkaisuus: (R - L) × ?N
Aputila: O(1)
Tehokas lähestymistapa: Yllä olevan lähestymistavan optimoimiseksi on tehtävä seuraava havainto numeroista, joilla on täsmälleen 5 tekijää.
Olkoon luvun alkulukujako p 1 a 1 ×p 2 a 2 × … ×p n a n .
Siksi tämän luvun tekijöiden määrä voidaan kirjoittaa muodossa (a 1 + 1)×(a 2 + 1) × … × (a n + 1).
Koska tämän tuotteen on oltava yhtä suuri 5 (joka on alkuluku ), tuotteessa saa olla vain yksi termi, joka on suurempi kuin 1. Tämän termin on oltava yhtä suuri kuin 5.
Siksi, jos a i + 1 = 5
=> a i = 4
Ratkaise ongelma noudattamalla alla olevia ohjeita:
- Vaadittu määrä on numeroiden määrä alueella, joka sisältää p 4 tekijänä, missä s on alkuluku.
- Tehokkaaseen laskemiseen p 4 laajalle valikoimalle ( [1, 10 18 ] ), ideana on käyttää Eratosthenes-seulaa kaikkien alkulukujen tallentamiseen aina 10 4.5 .
Alla on yllä olevan lähestymistavan toteutus:
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> >alkuluku;> // 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 && s <= 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 && s <= 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.> |
Python 3
# 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>>> |