Esecizi in php su moduli
This commit is contained in:
46
functions.php
Normal file
46
functions.php
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
function fattoriale ($input){
|
||||
|
||||
if ($input == 1){
|
||||
return 1;
|
||||
} else {
|
||||
return (fattoriale($input -1 ) * $input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function isPrime($num) {
|
||||
//1 is not prime. See: http://en.wikipedia.org/wiki/Prime_number#Primality_of_one
|
||||
if($num == 1)
|
||||
return false;
|
||||
|
||||
//2 is prime (the only even number that is prime)
|
||||
if($num == 2)
|
||||
return true;
|
||||
|
||||
/**
|
||||
* if the number is divisible by two, then it's not prime and it's no longer
|
||||
* needed to check other even numbers
|
||||
*/
|
||||
if($num % 2 == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the odd numbers. If any of them is a factor, then it returns false.
|
||||
* The sqrt can be an aproximation, hence just for the sake of
|
||||
* security, one rounds it to the next highest integer value.
|
||||
*/
|
||||
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
|
||||
if($num % $i == 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user