Created
March 3, 2020 14:22
-
-
Save TommyKolkman/369aff1fabaf50c923cc1595da361ef0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @desc Loop the primes as suggested by https://www.testingbrain.com/php-tutorial/a-prime-number-list-generation-php-script.html. | |
* @return array Primes and the sum of the primes up to and including that one. | |
*/ | |
function getPrimeArray(){ | |
$count = 0 ; | |
$prime = 2 ; | |
$table = []; | |
$total_count = 0; | |
$table_row_count = 0; | |
while ($count < 50 ) { | |
$div_count = 0; | |
/** | |
* We are now in the while loop and we will print a number. | |
* The number can only be divisible by one and itself. | |
*/ | |
for ( $i = 1; $i <= $prime; $i++) { | |
if ( ( $prime % $i ) == 0 ) { | |
$div_count++; | |
} | |
} | |
if ( $div_count < 3) { | |
$total_count = $total_count + $prime; | |
$table_row = [ | |
'prime' => $prime, | |
'prime_sum' => $total_count | |
]; | |
/** | |
* We want an empty table header after ten rows. | |
*/ | |
$table[] = $table_row; | |
if( $table_row_count == 9 ){ | |
$table[] = [ | |
'prime' => '<strong>Next set</strong>', | |
'prime_sum' => '<strong>Next set</strong>' | |
]; | |
$table_row_count = 0; | |
} | |
$table_row_count++; | |
/** | |
* Up the count so we can see when we reach 50. | |
*/ | |
$count = $count + 1; | |
} | |
$prime = $prime + 1; | |
} | |
return $table; | |
} | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" | |
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Prime counter</title> | |
<style> | |
table { | |
width: 100% | |
} | |
td { | |
border: 1px solid black; | |
width: 50%; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Primes</h1> | |
<table> | |
<?php if( $primeArray = getPrimeArray() ): ?> | |
<tr> | |
<td><h3>Prime</h3></td> | |
<td><h3>Prime sum</h3></td> | |
</tr> | |
<?php foreach( $primeArray as $primeTableRow ): ?> | |
<tr> | |
<td><?php echo $primeTableRow['prime'] ?></td> | |
<td><?php echo $primeTableRow['prime_sum'] ?></td> | |
</tr> | |
<?php endforeach; ?> | |
<?php endif; ?> | |
</table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment