PHP :: Aufgabe #202

1 Lösung Lösung öffentlich

Übung zu For-Schleifen

Anfänger - PHP von Blasius18 - 27.09.2018 um 18:41 Uhr
Die Fakultät einer Ganzzahl in den Grenzen von 0<=n<=60
als Konsolenanwendung umsetzen.


Der Anwender wird aufgefordet, eine Zahl in den Grenzen von 0 bis 60 einzugeben.
Das Programm errechnet den Wert und gibt ihn wieder aus.
Um die Entwicklung besser verfolgen zu können wird in jedem Schleifendurchgang der Aktuelle wert der Fakultät beim ,i-ten` schleifendurchgang ausgegeben.

Zusatzfeature (kann..)
gibt der Nutzer eine Zahl > 60 ein, gibt's einen Hinweis und er wird erneurt zur Eingabe einer Zahl 1 < zahl < 60 aufgefordert.
hat er 3 mal eine falsche Zahl eingegeben, bricht das Programm ab.

Lösungen:

vote_ok
von Exception (7090 Punkte) - 25.04.2019 um 18:13 Uhr
Hier ist meine Lösung zur Aufgabe.
Anmerkungen:
- Die Usereingabe wurde übersprungen. Es wurde mit den Werten, die in $numbers initial eingegeben wurden getestet. Diese wurden nachfolgend via array_filter noch bereinigt, sodass nur gültige Werte (laut Aufgabe 0...60) verwendet werden.
- In das Array $results werden alle berechneten Fakuläten geschrieben zusätzlich der jeweilige "User Input" sowie die einzelnen Schritte der Berechnung.
- Die Ergebnisse der Testwerte sind weiter unten aufgeführt.
Quellcode ausblenden PHP-Code
<?php

$numbers = array(5, 10, 0, 1, -1, 3, 60, 61, '13', 0.5, 7, 'Baum');

$numbers = array_filter($numbers, function ($value) {
    return ($value >= 0 && $value <= 60 && is_integer($value));
});

$results = array();

array_walk($numbers, function(&$value) use (&$results) {
    $results[]  =  array('Number' => $value, 'Steps' => array(), 'Faculty' => $value);
    $temp       =  array();

    while($value > 1) {
        $temp[] = $value--;
    }
    
    // Berechnung der Fakultät
    array_walk($temp, function(&$tempValue, $key) use (&$results) {
        if($key == 0)
            return;
        $results[sizeof($results)-1]['Steps'][]  = $results[sizeof($results)-1]['Faculty'] . " x " . $tempValue . " = " .  $results[sizeof($results)-1]['Faculty'] * $tempValue;
        $results[sizeof($results)-1]['Faculty'] *= $tempValue;
    });
});

echo '<pre>';
print_r($results);
echo '</pre>';

?>


Hier die Ausgabe zu den Testwerten:

Konsolenausgabe:


Array
(
[0] => Array
(
[Number] => 5
[Steps] => Array
(
[0] => 5 x 4 = 20
[1] => 20 x 3 = 60
[2] => 60 x 2 = 120
)

[Faculty] => 120
)

[1] => Array
(
[Number] => 10
[Steps] => Array
(
[0] => 10 x 9 = 90
[1] => 90 x 8 = 720
[2] => 720 x 7 = 5040
[3] => 5040 x 6 = 30240
[4] => 30240 x 5 = 151200
[5] => 151200 x 4 = 604800
[6] => 604800 x 3 = 1814400
[7] => 1814400 x 2 = 3628800
)

[Faculty] => 3628800
)

[2] => Array
(
[Number] => 0
[Steps] => Array
(
)

[Faculty] => 0
)

[3] => Array
(
[Number] => 1
[Steps] => Array
(
)

[Faculty] => 1
)

[4] => Array
(
[Number] => 3
[Steps] => Array
(
[0] => 3 x 2 = 6
)

[Faculty] => 6
)

[5] => Array
(
[Number] => 60
[Steps] => Array
(
[0] => 60 x 59 = 3540
[1] => 3540 x 58 = 205320
[2] => 205320 x 57 = 11703240
[3] => 11703240 x 56 = 655381440
[4] => 655381440 x 55 = 36045979200
[5] => 36045979200 x 54 = 1946482876800
[6] => 1946482876800 x 53 = 1.031635924704E+14
[7] => 1.031635924704E+14 x 52 = 5.3645068084608E+15
[8] => 5.3645068084608E+15 x 51 = 2.735898472315E+17
[9] => 2.735898472315E+17 x 50 = 1.3679492361575E+19
[10] => 1.3679492361575E+19 x 49 = 6.7029512571718E+20
[11] => 6.7029512571718E+20 x 48 = 3.2174166034424E+22
[12] => 3.2174166034424E+22 x 47 = 1.512185803618E+24
[13] => 1.512185803618E+24 x 46 = 6.9560546966426E+25
[14] => 6.9560546966426E+25 x 45 = 3.1302246134892E+27
[15] => 3.1302246134892E+27 x 44 = 1.3772988299352E+29
[16] => 1.3772988299352E+29 x 43 = 5.9223849687215E+30
[17] => 5.9223849687215E+30 x 42 = 2.487401686863E+32
[18] => 2.487401686863E+32 x 41 = 1.0198346916138E+34
[19] => 1.0198346916138E+34 x 40 = 4.0793387664554E+35
[20] => 4.0793387664554E+35 x 39 = 1.5909421189176E+37
[21] => 1.5909421189176E+37 x 38 = 6.0455800518868E+38
[22] => 6.0455800518868E+38 x 37 = 2.2368646191981E+40
[23] => 2.2368646191981E+40 x 36 = 8.0527126291133E+41
[24] => 8.0527126291133E+41 x 35 = 2.8184494201896E+43
[25] => 2.8184494201896E+43 x 34 = 9.5827280286448E+44
[26] => 9.5827280286448E+44 x 33 = 3.1623002494528E+46
[27] => 3.1623002494528E+46 x 32 = 1.0119360798249E+48
[28] => 1.0119360798249E+48 x 31 = 3.1370018474572E+49
[29] => 3.1370018474572E+49 x 30 = 9.4110055423715E+50
[30] => 9.4110055423715E+50 x 29 = 2.7291916072877E+52
[31] => 2.7291916072877E+52 x 28 = 7.6417365004057E+53
[32] => 7.6417365004057E+53 x 27 = 2.0632688551095E+55
[33] => 2.0632688551095E+55 x 26 = 5.3644990232848E+56
[34] => 5.3644990232848E+56 x 25 = 1.3411247558212E+58
[35] => 1.3411247558212E+58 x 24 = 3.2186994139709E+59
[36] => 3.2186994139709E+59 x 23 = 7.403008652133E+60
[37] => 7.403008652133E+60 x 22 = 1.6286619034693E+62
[38] => 1.6286619034693E+62 x 21 = 3.4201899972854E+63
[39] => 3.4201899972854E+63 x 20 = 6.8403799945709E+64
[40] => 6.8403799945709E+64 x 19 = 1.2996721989685E+66
[41] => 1.2996721989685E+66 x 18 = 2.3394099581432E+67
[42] => 2.3394099581432E+67 x 17 = 3.9769969288435E+68
[43] => 3.9769969288435E+68 x 16 = 6.3631950861496E+69
[44] => 6.3631950861496E+69 x 15 = 9.5447926292244E+70
[45] => 9.5447926292244E+70 x 14 = 1.3362709680914E+72
[46] => 1.3362709680914E+72 x 13 = 1.7371522585188E+73
[47] => 1.7371522585188E+73 x 12 = 2.0845827102226E+74
[48] => 2.0845827102226E+74 x 11 = 2.2930409812449E+75
[49] => 2.2930409812449E+75 x 10 = 2.2930409812449E+76
[50] => 2.2930409812449E+76 x 9 = 2.0637368831204E+77
[51] => 2.0637368831204E+77 x 8 = 1.6509895064963E+78
[52] => 1.6509895064963E+78 x 7 = 1.1556926545474E+79
[53] => 1.1556926545474E+79 x 6 = 6.9341559272845E+79
[54] => 6.9341559272845E+79 x 5 = 3.4670779636422E+80
[55] => 3.4670779636422E+80 x 4 = 1.3868311854569E+81
[56] => 1.3868311854569E+81 x 3 = 4.1604935563707E+81
[57] => 4.1604935563707E+81 x 2 = 8.3209871127414E+81
)

[Faculty] => 8.3209871127414E+81
)

[6] => Array
(
[Number] => 7
[Steps] => Array
(
[0] => 7 x 6 = 42
[1] => 42 x 5 = 210
[2] => 210 x 4 = 840
[3] => 840 x 3 = 2520
[4] => 2520 x 2 = 5040
)

[Faculty] => 5040
)

)
1811034

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.