priemgetallen.php

Gesponsorde koppelingen

PHP script bestanden

  1. priemgetallen.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php

set_time_limit(0); // Om timeouts te vermijden bij grote getallen

// Loadtime bepalen

$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];

?>


<html>
<head>
<style type="text/css">
body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
}

table tr td {
    font-size: 12px;
    padding: 5px;
}

fieldset {
    border: 1px solid #555;
    width: 400px;
    margin: 5px;
    display: inline-block;
    vertical-align: top;
}

legend {
    padding: 5px;
}

input {
    border: 1px solid #CCC;
    padding: 2px;
}

hr {
    background: #EEE;
    border: 0;
    height: 1px;
}
    
</style>
<title>Priemgetallen</title>
</head>
<body>
<form action="" method="post">
    <fieldset>
        <legend>Check Priemgetal</legend>
        <table>
            <tr>
                <td>Getal</td>
                <td><input type="text" size="4" name="number" /></td>
                <td><input type="submit" name="check" value="Controleer Getal" /></td>
              </tr>
          </table>
        <?php
        
            if($_SERVER['REQUEST_METHOD'] == "POST") {
                $number = $_POST['number'];
                if(isset($_POST['check']) && !empty($number) && is_numeric($number)) {
                    $gevondenDelers = 0;
                    for($i = 1; $i <= $number; $i++) {
                        $result = $number / $i;
                        if(is_int($result)) {
                            $gevondenDelers++;
                        }
                    }

                    if($gevondenDelers == 2) { // Precies 2 delers, 1 en zichzelf
                        echo '<span style="background: #DFFFE3; padding: 2px;">Het getal <b>' . $number . '</b> is een priemgetal.</span>';
                    }

                    else {
                        echo '<span style="background: #FFDFE3; padding: 2px;">Het getal <b>' . $number . '</b> is geen priemgetal.</span>';
                    }
                }
            }

            
        ?>

       </fieldset>
    
    <fieldset>
        <legend>Lijsten Priemgetallen</legend>
        <table>
            <tr>
                <td>Van</td>
                <td><input type="text" size="4" name="number1" /></td>
                <td>Tot</td>
                <td><input type="text" size="4" name="number2" /></td>
                <td><input type="submit" name="lijst" value="Maak Lijst" /></td>
               </tr>
           </table>
        <?php
        
            if($_SERVER['REQUEST_METHOD'] == "POST") {
                $number1 = $_POST['number1'];
                $number2 = $_POST['number2'];
                $columns = 10;
                if(isset($_POST['lijst']) && !empty($number1) && !empty($number2) && is_numeric($number1) && is_numeric($number2)) {
                    $i = $number1;
                    $totalNumbers = $number2 - $number1 + 1;
                    $tableDesignCount = 1;
                    $priemgetal = 0;
                    echo '<table style="width: 100%;">';
                    while($i <= $number2) {
                        if($tableDesignCount == 1) {
                            echo '<tr>';
                        }

                        $gevondenDelers = 0;
                        for($i2 = 1; $i2 <= $i; $i2++) {
                            $result = $i / $i2;
                            if(is_int($result)) {
                                $gevondenDelers++;
                            }
                        }

                        
                        echo '<td><span style="padding: 2px; background: #';
                        if($gevondenDelers == 2) {
                            echo 'DFFFE3';
                            $priemgetal++;
                        }

                        else {
                            echo 'FFDFE3';
                        }

                        echo '">' .  $i++ . '</span></td>';
                        if($tableDesignCount == $columns) {
                            echo '</tr>';
                            $tableDesignCount = 0;
                        }

                        $tableDesignCount++;
                    }

                    echo '</table>';
                    echo '<p>Er zijn ' . $priemgetal . ' getallen van de ' . $totalNumbers . ' (' . round(($priemgetal/$totalNumbers)*100) . '%) getallen priem.</p>';
                }
            }

            
        ?>

       </fieldset>
</form>
<?php
$endtime
= microtime();
$endarray = explode(" ", $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime - $starttime;
$totaltime = round($totaltime, 3);
echo "<center>Loadtime: " . $totaltime . " seconds.";
?>

</body>
</html>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.