Created
February 27, 2020 02:48
-
-
Save adibenc/f165e3ad7efc6ac9039411e06b82c390 to your computer and use it in GitHub Desktop.
perfect num
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 | |
| // $get=$_GET; | |
| $post=$_POST; | |
| $num=isset($post['num'])?$post['num']:0; | |
| ?> | |
| <form method="post" action=""> | |
| <div> | |
| <label for="">Num</label> | |
| <!-- <input type="text" name="num" value=""> --> | |
| <textarea name="num" id="" cols="30" rows="10"><?=$num?></textarea> | |
| </div> | |
| <input type="submit"> | |
| </form> | |
| <?php | |
| function preout($v=[]){ | |
| echo "<pre>"; | |
| var_dump($v); | |
| echo "</pre>"; | |
| } | |
| function isPerfect($n){ | |
| $sum = 1; | |
| for ($i = 2; $i * $i <= $n; $i++) { | |
| if ($n % $i == 0) { | |
| if($i * $i != $n) | |
| $sum = $sum + $i + (int)($n / $i); | |
| else | |
| $sum = $sum + $i; | |
| } | |
| } | |
| // echo "sum ".$sum; | |
| // echo "<br>"; | |
| // If sum of divisors is equal to | |
| // n, then n is a perfect number | |
| if ($sum == $n && $n != 1){ | |
| return "perfect"; | |
| // return true; | |
| }else if($n-$sum==1){ | |
| return "hampir"; | |
| }else{ | |
| return "bukan"; | |
| } | |
| return false; | |
| } | |
| // Driver Code | |
| function test(){ | |
| echo "Below are all perfect numbers till 10000\n"; | |
| echo "<br>"; | |
| for ($n = 2; $n < 100; $n++){ | |
| if (isPerfect($n)){ | |
| echo "$n is a perfect number\n"; echo "<br>"; | |
| } | |
| } | |
| } | |
| function cek($v=null){ | |
| if($v==null){ | |
| return; | |
| } | |
| $txt=preg_split('/\n/', $v); | |
| $q=(int)$txt[0]; | |
| if($q<1||$q>50){ | |
| echo "q<1||q>50 !!"; | |
| return; | |
| } | |
| $out=""; | |
| for($i=1;$i<=$q;$i++){ | |
| $out.=isPerfect((int)$txt[$i])."<br>"; | |
| } | |
| echo $out; | |
| } | |
| cek($num); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment