-
Notifications
You must be signed in to change notification settings - Fork 0
live_programming
Op deze pagina vind je de opdrachten die de docenten van de cursus EMS10 in les 3 van week 2 in een live-programmeerles hebben gedemonstreerd.
Een getal is een narcistisch getal als het de som is van de losse cijfers van dat getal elk verheven tot de macht van het aantal cijfers van dat getal. Zo is het getal 371 bijvoorbeeld een narcistisch getal want: 3**3 + 7**3 + 1**3 = 27 + 343 + 1 = 371. Maar 372 is geen narcistisch getal want: 3**3 + 7**3 + 2**3 = 27 + 343 + 8 = 378 != 372. Een ander voorbeeld van een narcistisch getal is 8208 want: 8**4 + 2**4 + 0**4 + 8**4 = 4096 + 16 + 0 + 4096 = 82082.
Zie: http://mathworld.wolfram.com/NarcissisticNumber.html voor nog veel meer voorbeelden.
Schrijf een functie genaamd is_narcistisch met een parameter genaamd
getal die de booleaanse waarde True teruggeeft als het als argument meegegeven getal een narcistisch getal is maar die de booleaanse waarde False
teruggeeft als dit niet zo is.
Tip: het is handig om eerst een functie genaamd aantal_cijfers te schrijven die het aantal cijfers van een als argument meegegeven getal bepaald.
Schrijf een functie genaamd kleinste_narcistische_getal_met_n_cijfers met een
parameter genaamd n die het kleinste narcistische getal met n cijfers bepaalt
en teruggeeft.
Bepaal de kleinste narcistische getallen met 1 tot en met 7 cijfers. De juiste uitvoer is:
Het kleinste narcistische getal met 1 cijfers: 0
Het kleinste narcistische getal met 2 cijfers: Bestaat niet
Het kleinste narcistische getal met 3 cijfers: 153
Het kleinste narcistische getal met 4 cijfers: 1634
Het kleinste narcistische getal met 5 cijfers: 54748
Het kleinste narcistische getal met 6 cijfers: 548834
Het kleinste narcistische getal met 7 cijfers: 1741725
De code kun je hier downloaden.
#!Python
def aantal_cijfers(getal):
cijfers = 1
getal = getal // 10
while (getal > 0):
cijfers = cijfers + 1
getal = getal // 10
return cijfers
if aantal_cijfers(135) != 3:
print('Error: aantal_cijfers(135) != 3')
if aantal_cijfers(153) != 3:
print('Error: aantal_cijfers(153) != 3')
if aantal_cijfers(4679307774) != 10:
print('Error: aantal_cijfers(4679307774) != 10')
if aantal_cijfers(115132219018763992565095597973971522400) != 39:
print('Error: aantal_cijfers(115132219018763992565095597973971522400) != 39')
#!Python
def aantal_cijfers_3(getal):
if getal == 0:
return 1
return int(math.log10(getal))+1
#!Python
def is_narcistisch(getal):
begin_getal = getal
n = aantal_cijfers(getal)
som = 0
for i in range(n):
cijfer = getal % 10
som = som + cijfer**n
getal = getal // 10
return som == begin_getal
if is_narcistisch(135) != False:
print('Error: is_narcistisch(135) != False')
if is_narcistisch(153) != True:
print('Error: is_narcistisch(153) != True')
if is_narcistisch(4679307774) != True:
print('Error: is_narcistisch(4679307774) != True')
if is_narcistisch(115132219018763992565095597973971522400) != True:
print('Error: is_narcistisch(115132219018763992565095597973971522400) != True')
if is_narcistisch(1151322190187639925650955979739715224002) != False:
print('Error: is_narcistisch(1151322190187639925650955979739715224002) != False')
De code kun je hier downloaden.
#!Python
def kleinste_narcistische_getal_met_n_cijfers(n):
if (n == 1):
return 0
for kandidaat in range(10 ** (n - 1), 10 ** n):
if is_narcistisch(kandidaat):
return kandidaat
return 'Bestaat niet'
if kleinste_narcistische_getal_met_n_cijfers(3) != 153:
print('Error: kleinste_narcistische_getal_met_n_cijfers(3) != 153')
if kleinste_narcistische_getal_met_n_cijfers(5) != 54748:
print('Error: kleinste_narcistische_getal_met_n_cijfers(3) != 54748')
De code kun je hier downloaden.
#!Python
for aantalcijfers in range(1, 8):
print('Het kleinste narcistische getal met', aantalcijfers, 'cijfers:', kleinste_narcistische_getal_met_n_cijfers(aantalcijfers))