Python Nozioni di baseFacile

Massimo fattore comune (HCF)

Guida dettagliata e implementazione Python per il problema del "fattore comune più alto (HCF)".

Dichiarazione del problema

Facile

Scrivi una funzione find_hcf(a, b) che accetta due interi positivi a e b e restituisce il loro massimo comune fattore (HCF). L'HCF è il più grande intero positivo che divide entrambi i numeri senza lasciare resto.

Ad esempio, l'HCF di 12 e 18 è 6, perché 6 è il numero più grande che divide equamente sia 12 che 18.

Vincoli
  • 1 <= a, b <= 10^6

Esempi

Example 1
Input
find_hcf(12, 18)
Output
6
Explanation

Factors of 12: 1, 2, 3, 4, 6, 12. Factors of 18: 1, 2, 3, 6, 9, 18. Common factors: 1, 2, 3, 6. Highest is 6.

Example 2
Input
find_hcf(24, 36)
Output
12
Explanation

Factors of 24: 1, 2, 3, 4, 6, 8, 12, 24. Factors of 36: 1, 2, 3, 4, 6, 9, 12, 18, 36. Highest common factor is 12.

Example 3
Input
find_hcf(7, 13)
Output
1
Explanation

7 and 13 are both prime numbers with no common factors other than 1.

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.