Advertisement
ShadowEmbrace

Top Numbers

Oct 29th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. <?php
  2.  
  3. $num = intval(readline());
  4.  
  5. $topNums = findTopNumber($num);
  6. echo $topNums;
  7.  
  8. function findTopNumber(int $n)
  9. {
  10.     $result = [];
  11.     for ($i = 1; $i <= $n; $i++) {
  12.         $isOdd = false;
  13.         $sum = 0;
  14.         $numberAsString = strval($i);
  15.  
  16.         for ($k = 0; $k < strlen($numberAsString); $k++){
  17.             $digit = intval($numberAsString[$k]);
  18.             $sum += $digit;
  19.             if ($digit % 2 === 1){
  20.                 $isOdd = true;
  21.             }
  22.         }
  23.         if ($sum % 8 === 0 && $isOdd) {
  24.             $result[] = $i;
  25.         }
  26.     }
  27.     $result = implode(" " . PHP_EOL, $result);
  28.     return $result;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement