# Live programming Op deze pagina vind je de opdrachten die de docenten van de cursus [EMS10](Home.md) in les 3 van week 2 in een live-programmeerles hebben gedemonstreerd. ## Narcistische getallen 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](http://mathworld.wolfram.com/NarcissisticNumber.html) voor nog veel meer voorbeelden. ## Opdracht 1 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 bepaalt. ## Opdracht 2 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. ## Opdracht 3 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 ``` ## Uitwerking opdracht 1 De code kun je [hier](https://bitbucket.org/HR_ELEKTRO/ems10/raw/master/live_programming/progs/narcistisch_getal.py) downloaden. ### Functie `aantal_cijfers` ```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') ``` ### Alternatieve functie `aantal_cijfers` ```python import math def aantal_cijfers(getal): if getal == 0: return 1 return int(math.log10(getal)) + 1 ``` ### Alternatieve functie `aantal_cijfers` ```python def aantal_cijfers(getal): return len(str(getal)) ``` ### Functie `is_narcistisch` ```python def is_narcistisch(getal): begin_getal = getal n = aantal_cijfers(getal) som = 0 for i in range(n): # isoleer het laatste cijfer cijfer = getal % 10 som = som + cijfer ** n # verwijder het laatste cijfer 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') ``` ### Alternatieve functie `is_narcistisch` ```python def is_narcistisch(getal): begin_getal = getal n = aantal_cijfers(getal) som = 0 for i in range(n-1,-1,-1): # isoleer het eerste cijfer cijfer = getal // 10**i som = som + cijfer ** n # verwijder het eerste cijfer getal = getal % 10**i return som == begin_getal ``` ## Uitwerking opdracht 2 De code kun je [hier](https://bitbucket.org/HR_ELEKTRO/ems10/raw/master/live_programming/progs/narcistisch_getal.py) 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 None 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') ``` ## Uitwerking opdracht 3 De code kun je [hier](https://bitbucket.org/HR_ELEKTRO/ems10/raw/master/live_programming/progs/narcistisch_getal.py) downloaden. ```python for aantalcijfers in range(1, 8): resultaat = kleinste_narcistische_getal_met_n_cijfers(aantalcijfers) print('Het kleinste narcistische getal met', aantalcijfers, 'cijfers:', end = ' ') if resultaat == None: print('Bestaat niet', ) else: print(resultaat) ```