Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active May 20, 2020 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/1d81d9b1a0c536844bec5b4a7ba2e649 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/1d81d9b1a0c536844bec5b4a7ba2e649 to your computer and use it in GitHub Desktop.
Just PHP FUN 002.
<?php
# https://www.codewars.com/kata/544aed4c4a30184e960010f4 Find the divisors!.
function divisors($integer) {
$answer = [];
for($i = 2; $i < $integer; $i++)
if(0 == $integer%$i) array_push($answer,$i);
if(empty($answer)) return "$integer is prime";
return $answer;
}
<?php
# https://www.codewars.com/kata/5841f680c5c9b092950001ae Series of integers from m to n.
function generate_integers(int $m, int $n): array {
return range($m,$n);
}

Just PHP FUN 002.

Started at 20:48 20.05.2020 Wednesday May.
Finised at 22:11 20.05.2020 Wednesday May. (1hr 37minutes).

<?php
# https://www.codewars.com/kata/51675d17e0c1bed195000001 Largest 5 digit number in a series.
function solution(string $s): int {
$array = str_split($s);
$max = intval(join('',array_slice($array,0,5)));
$cur = $max;
for($i=5; $i < count($array); $i++){
$cur = ($cur*10)%(10**5) + (int)$array[$i];
if($max < $cur) $max = $cur;
}
return $max;
}
<?php
# https://www.codewars.com/kata/582c297e56373f0426000098 Convert a linked list to a string.
function stringify($list): string {
$ans = "";
while($list){
$tmp = $list->data;
$ans .= "$tmp -> ";
$list = $list->next;
}
$ans .= "NULL";
return $ans;
}
<?php
# https://www.codewars.com/kata/559e3224324a2b6e66000046 Functions of Integers on Cartesian Plane.
function suma($n){
return (($n+1)*$n)/2;
}
function sumin($n) {
$z = suma($n) * $n;
$tmp = 0;
for($i=1; $i < $n; $i++){
$tmp += $i;
$z -= $tmp;
}
return $z;
}
function sumax($n) {
$z = suma($n) * $n;
$tmp = 0;
for($i=1; $i < $n; $i++){
$tmp += $i;
$z += $tmp;
}
return $z;
}
function sumsum($n) {
return sumin($n)+sumax($n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment