Python Nozioni di baseFacile

Trovare il numero di interi che ha esattamente x divisori

Guida dettagliata e implementazione Python per il problema 'Trovare il numero di interi che ha esattamente x divisori'.

Dichiarazione del problema

Facile

Scrivi una funzione count_with_x_divisors(n, x) che accetta due numeri interi positivi n e x e restituisce il conteggio dei numeri interi nell'intervallo [1, n] (incluso) che hanno esattamente x divisori.

Un divisore di un numero k è qualsiasi numero intero che divide k in modo uniforme. Ad esempio, i divisori di 6 sono 1, 2, 3, 6 (4 divisori).

Vincoli
  • 1 <= n <= 1000
  • 1 <= x <= 50

Esempi

Example 1
Input
count_with_x_divisors(10, 2)
Output
4
Explanation

Numbers from 1 to 10 with exactly 2 divisors (i.e., prime numbers): 2, 3, 5, 7. That's 4 numbers.

Example 2
Input
count_with_x_divisors(10, 1)
Output
1
Explanation

Only the number 1 has exactly 1 divisor.

Example 3
Input
count_with_x_divisors(20, 4)
Output
5
Explanation

Numbers from 1-20 with exactly 4 divisors: 6(1,2,3,6), 8(1,2,4,8), 10(1,2,5,10), 14(1,2,7,14), 15(1,3,5,15). That's 5 numbers.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Numbers come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.