I'm new in PHP. How can i achieve continously loop with adding different value?
it's something like this
<?php
$gap1 = 2;
$gap2 = 3;
$lenght = 10;
for( $i=0; $i<$length; $i++ )
{
//the code
}
?>
and the result will be : 0 2 5 7 10 12 15 17
thank you for your help :)
$gap1=2;
$gap2=3;
$lenght = 10;
$p=0;
for($i=0;$i<$lenght;$i++)
{
if($i==0){$p=0;}
elseif($i%2==0)
{
$p+=$gap2;
}
else{
$p+=$gap1;
}
echo $p.'<br>';
}
Try this code:
$gap1 = 2;
$gap2 = 3;
$length = 10;$i=0;
$x = 0;
while($i<$length)
{
echo $x." ";
if($i%2 == 0)
$x+=$gap1;
else
$x+=$gap2;
$i++;
}
Output:
0 2 5 7 10 12 15 17 20 22
$gap = array(2, 3);
$result = array(-1 => 0);
$length = 10;
for($i = 0; $i < $length; $i++) {
$result[] = $result[$i-1] + $gap[($i) % count($gap)];
}
echo implode(' ', $result);
Related
I have a PHP code. The code is supposed to print the output followed by a new line.
The code works fine but i have unneccesary new line at the end. There should be only one newline at the end, but my code prints several new lines. What could be the issue? Please help.
<?php
/* Read input from STDIN. Print your output to STDOUT*/
$fp = fopen("php://stdin", "r");
//Write code here
$loop = 0;
$n = 0; $arr = [];
while(!feof($fp)) {
$arr = []; $n = 0;
if($loop == 0) {
$total = fgets($fp);
}
else {
if($loop%2 == 1) {
$n = fgets($fp);
}
else {
$arr = fgets($fp);
}
}
if($loop > 0 && $loop%2 == 0) {
$arr = explode(" ", $arr);
$m = [];
for($i = 0; $i < 1<<10; $i++) {
$m[$i] = -1;
}
$n = count($arr);
$r = 0;
for($i = 0; $i < 1<<10; $i++) {
$r = max($r, fd_sum($i, $m, $arr, $n));
}
echo $r."\n";
}
$loop++;
}
fclose($fp);
?>
<?php
function fd_sum($i, $m, $arr, $n) {
if($i == 0) {
return $m[$i] = 0;
}
else if($m[$i] != -1) {
return $m[$i];
}
else {
$rr = 0;
for($j = 0; $j < $n; $j++) {
$num = (int)$arr[$j];
$b = save($num);
if(($i | $b) == $i) {
$z = $i^save($num);
$y = fd_sum($z, $m, $arr, $n);
$v = ($y + $num);
$rr = max($v, $rr);
}
}
return $m[$i] = $rr;
}
}
?>
<?php
function save($nm)
{
$x = 0;
for($i = 1; $nm/$i > 0; $i *= 10) {
$d = ($nm/$i) % 10;
$x = $x | (1 << $d);
}
return $x-1;
}
?>
My input is
3
4
3 5 7 2
5
121 3 333 23 4
7
32 42 52 62 72 82 92
My output is
17
458
92
-
-
-
-
The expected output is
17
458
92
-
Note : I have used '-' to indicate a new line
What am i doing wrong? Please help.
The PHP interpreter is reading the new lines after the closing tags and just spitting it right back out as output. Removing the extra opening/closing tags should remove the extra new lines.
Also, php closing tags are not necessary and i recommend omitting them.
<?php
/* Read input from STDIN. Print your output to STDOUT*/
$fp = fopen("php://stdin", "r");
//Write code here
$loop = 0;
$n = 0; $arr = [];
while(!feof($fp)) {
$arr = []; $n = 0;
if($loop == 0) {
$total = fgets($fp);
}
else {
if($loop%2 == 1) {
$n = fgets($fp);
}
else {
$arr = fgets($fp);
}
}
if($loop > 0 && $loop%2 == 0) {
$arr = explode(" ", $arr);
$m = [];
for($i = 0; $i < 1<<10; $i++) {
$m[$i] = -1;
}
$n = count($arr);
$r = 0;
for($i = 0; $i < 1<<10; $i++) {
$r = max($r, fd_sum($i, $m, $arr, $n));
}
echo $r."\n";
}
$loop++;
}
fclose($fp);
function fd_sum($i, $m, $arr, $n) {
if($i == 0) {
return $m[$i] = 0;
}
else if($m[$i] != -1) {
return $m[$i];
}
else {
$rr = 0;
for($j = 0; $j < $n; $j++) {
$num = (int)$arr[$j];
$b = save($num);
if(($i | $b) == $i) {
$z = $i^save($num);
$y = fd_sum($z, $m, $arr, $n);
$v = ($y + $num);
$rr = max($v, $rr);
}
}
return $m[$i] = $rr;
}
}
function save($nm)
{
$x = 0;
for($i = 1; $nm/$i > 0; $i *= 10) {
$d = ($nm/$i) % 10;
$x = $x | (1 << $d);
}
return $x-1;
}
I want to divide a number into n number of parts like follows
Input : $n = 4;$m =14;
Output should as : array(1=>4,2=>4,3=>3,4=>3);
i.e :
$n $m
1 1+1+1+1
2 1+1+1+1
3 1+1+1
4 1+1+1
Any suggestions or links would help a lot.
Here is one way to do it - this works in phpfiddle:
$n = 4;
$m =14;
$array = distribute($m,$n);
print_r($array);
function distribute($m,$n) {
$div = floor($m / $n);
$mod = fmod($m, $n);
$result = array();
for ($i = 1;$i <= $n;$i++) {
$result[$i] = $div;
}
if ($mod > 0) {
for ($i = 1;$i <= $mod;$i++) {
$result[$i] = $result[$i] + 1;
}
}
return $result;
}
Just started learning. Here's what I have:
<?php
$i = 0;
$num = $i * 12;
for ($i=0; $i<13; $i++) {
echo($i." times 12 = ".$num."<br>");
}
?>
The outcome should be:
1 times 12 = 12
2 times 12 = 24
3 times 12 = 36
etc...
The outcome I actually get is:
1 times 12 = 0
2 times 12 = 0
3 times 12 = 0
any ideas?
It is because you have this declaration before for loop:
$i = 0; $num = $i * 12;
so always $num will be 0. Just place it into for:
for ($i=1; $i<13; $i++) {
$num = $i*12;
echo($i." times 12 = ".$num."<br>");
}
You don't need declare $i variable before for loop. This variable will be overwritten. There is simple test:
$i = 5;
for($i = 1; $i<10; $i++);
echo $i;
OUTPUT:
10
<?php
$i = 1; $num = 1;
for ($i=1; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
If you want that result you should put the calculation inside the for loop and start i with 1
<?php
for ($i=1; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
Something like this
<?php
$num = 12;
for ($i=1; $i<13; $i++) {
echo("$i times 12 = ".$num*$i);
echo "<br>";
}
?>
<?php
for ($i=0; $i<13; $i++) {
echo($i." times 12 = ".$i*12."<br>");
}
?>
Line $num = $i * 12; shift into loop
<?php
$i = 0;
for ($i=0; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
Your $num variable only ever increments on $i when its 0, try putting it in the for loop like this.
$i = 0;
for ($i=0; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
i need help for this task.
i need a for loop who picks the closest duration
sample we have a duration with 120 seconds the 120 seconds are on every calculation divided by 8
so here is an example
120 Duration
Here are the 8 closest values
15
30
45
60
75
90
105
120
How i can realize this i have all tested
<?php
$count = 1;
$duration = 120;
$temp = 0;
for ($x = 1; $x < 120; $x++) {
#$j = $x / 8;
$temp = $x / 8;
echo '<pre>' . ($temp) . '</pre>';
if ($count == 8) {
break;
}
$count++;
}
?>
Your entire loop is totally redundant. Why not just this:
<?php
for ($i = 0; i < 8; ++$i)
{
echo '<pre>' . (15 * ($i+1)) . '</pre>';
}
?>
You can use $i directly as a counter in the loop.
Your question is quite unclear and the provided code doesn't go from 1->120 since you break it after 8 iterations.
To get the values 15 30 45 60 75 90 105 120 from the base 120 you would need something like this:
$result = array();
$duration = 120; //the duration in seconds as provided in the example
$divider = 8; //the divider 8 as provided by the example
for ($i = 1; $i <= $divider; $i++) {
//This will give 1* 120/8 = 15 for the first run
//2* 120/8 = 30 for the second and so on
$result[] = (int) $i * $duration / $divider;
}
would the following work?
function sample($max, $count) {
$samples = array();
for($i = 1; $i <= $count; ++i) {
$samples[] = (int)($max / $count * $i);
}
return $samples;
}
Do you mean something like
$result = array();
for ($i = 1; $i <= 8; $i++) {
$result[] = (int) ($duration / 8) * $i;
}
?
Say I create a recursive array with this code:
$digits = 0;
$tens = 0;
$hundreds = 0;
for($i = 0; $i <= 100; $i++)
{
$myArray[$hundreds][$tens][$digits] = $i;
$digits++;
if($digits > 9)
{
$digits = 0;
$tens++;
}
if($tens > 9)
{
$tens = 0;
$hundreds++;
}
}
how could I echo out all the data fromt the 'tens array' == 2?
To be clear, I'd be looking for these results:
20 21 22 23 24 25 26 27 28 29
since im using base 10, I could just do this:
for($i=0; $i < 10; $i++)
{
echo $myArray[0][2][$i]
}
but what if i have no idea how many elements are in the digits array?
foreach($myArray[0][2] as $v) {
echo $v."<br>\n";
}
<?php
$tensArr = $myArray[0][2];
for($i= 0 ; $i < count($tensArr); $i++)
{
echo $tensArr[$i]."\n" ;
}