note 38353 added to control-structures.for
| From: | omert at geb-team dot de | Date: | Wed, 17 Dec 2003 19:21:59 +0000 |
| Subject: | note 38353 added to control-structures.for | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62139@lists.php.net to get a copy of this message | ||
If someone needs to find whether a number is a prime number or not, he/she can use the following
script :
<?
$j="2"
$input = "100" //or any other number.
for($x=2;$x < $input ;$x++){
if ($input % $x == 0 )
{
echo "no, $input is not a prime, because it can be divided over $x . " ;
$prime="0";
break ;
}
else
{
$prime = "1" ;}
}
if ($prime == 1 )
echo "yes, $input is a prime ." ;
else
echo " " ;
// I know this is not the best way, but it wasn't working another way.
?>
You can simply get $input with this html-script:
<form name="wert" method="post" action="">
<input type="text" name="input">
<input type="submit" value="Is it a prime ?" >
</form>
You should insert in action the name of your php file.
There is only one problem, that it takes time and you get a time out, when you have a very large
number.
----
Manual Page -- http://www.php.net/manual/en/control-structures.for.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38353
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38353&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38353&report=yes
Search -- http://master.php.net/manage/user-notes.php