Accademia:Projecteuler 1.py: differenze tra le versioni
Vai alla navigazione
Vai alla ricerca
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
Riga 1: | Riga 1: | ||
{{Template:Acc_Menu_Python}} | |||
If we list all the natural numbers below 10 that are multiples of 3 or 5, | |||
we get 3, 5, 6 and 9. The sum of these multiples is 23. | we get 3, 5, 6 and 9. The sum of these multiples is 23. | ||
Find the sum of all the multiples of 3 or 5 below 1000. | Find the sum of all the multiples of 3 or 5 below 1000. | ||
<pre> | |||
def multiple(multiple,value): | def multiple(multiple,value): | ||
multiples = [] | multiples = [] | ||
Riga 19: | Riga 19: | ||
return sum_of_numbers | return sum_of_numbers | ||
if __name__ == '__main__': | if __name__ == '__main__': | ||
somma = 0 | somma = 0 | ||
Riga 34: | Riga 32: | ||
valore = int(input('value: ')) | valore = int(input('value: ')) | ||
print(multiple(multiplo,valore)) | print(multiple(multiplo,valore)) | ||
</pre> |
Versione delle 19:26, 15 mag 2011
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
def multiple(multiple,value): multiples = [] for number in range(multiple, value, multiple): multiples.append(number) return multiples def sum_of_numbers(the_range): sum_of_numbers = 0 for number in range(the_range): if not number % 3 or not number % 5: sum_of_numbers += number return sum_of_numbers if __name__ == '__main__': somma = 0 for i in range(10): # inutile controllare 0,1,2 sappiamo già che non vanno bene così si risparmiano preziosi nanosecondi. if i % 3 == 0 or i % 5 == 0: somma += i print(somma) the_range = int(input('range: ')) print(sum_of_numbers(the_range)) multiplo = int(input('multiple: ')) valore = int(input('value: ')) print(multiple(multiplo,valore))