Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Recamans-sequence/PHP/recamans-sequence.php
Normal file
45
Task/Recamans-sequence/PHP/recamans-sequence.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
$a = array();
|
||||
array_push($a, 0);
|
||||
|
||||
$used = array();
|
||||
array_push($used, 0);
|
||||
|
||||
$used1000 = array();
|
||||
array_push($used1000, 0);
|
||||
|
||||
$foundDup = false;
|
||||
$n = 1;
|
||||
|
||||
while($n <= 15 || !$foundDup || count($used1000) < 1001) {
|
||||
$next = $a[$n - 1] - $n;
|
||||
if ($next < 1 || in_array($next, $used)) {
|
||||
$next += 2 * $n;
|
||||
}
|
||||
$alreadyUsed = in_array($next, $used);
|
||||
array_push($a, $next);
|
||||
if (!$alreadyUsed) {
|
||||
array_push($used, $next);
|
||||
if (0 <= $next && $next <= 1000) {
|
||||
array_push($used1000, $next);
|
||||
}
|
||||
}
|
||||
if ($n == 14) {
|
||||
echo "The first 15 terms of the Recaman sequence are : [";
|
||||
foreach($a as $i => $v) {
|
||||
if ( $i == count($a) - 1)
|
||||
echo "$v";
|
||||
else
|
||||
echo "$v, ";
|
||||
}
|
||||
echo "]\n";
|
||||
}
|
||||
if (!$foundDup && $alreadyUsed) {
|
||||
printf("The first duplicate term is a[%d] = %d\n", $n, $next);
|
||||
$foundDup = true;
|
||||
}
|
||||
if (count($used1000) == 1001) {
|
||||
printf("Terms up to a[%d] are needed to generate 0 to 1000\n", $n);
|
||||
}
|
||||
$n++;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue