Yes it's about loops. I have the below script and the first loop there is no problem as it mixes the variables but on the second loop I can't seem to figure out how to find the value of $c. Does anybody know because this is an annoying puzzle I have been trying to figure out. I got everything else correct except for finding the value of $c in the second loop. Please help.

<?php
$a=1;
$b=47;
$c=88;
$tmp=23;
for($i=0;$i<80;$i++) {
$random_number=floor(mt_rand(0,1000));
echo $random_number.'<br>';
$tmp=($tmp+$c+$random_number)*0.5;
$c=$b;
$b=$a;
$a=$tmp;
}


//Now to reverse
echo '<hr>';


$tmp=$a;
for ($i=0;$i<80;$i--) {
$a=$b;
$b=$c;
//$c=(unknown);
//$random_number=(($tmp-$c)-$a)*2;
//$tmp=($tmp-(($random_number*0.5)+$c+$a))*2;
//echo $random_number.'<br>';
}
?>

Yes it's about loops. I have the below script and the first loop there is no problem as it mixes the variables but on the second loop I can't seem to figure out how to find the value of $c. Does anybody know because this is an annoying puzzle I have been trying to figure out. I got everything else correct except for finding the value of $c in the second loop. Please help.

<?php
$a=1;
$b=47;
$c=88;
$tmp=23;
for($i=0;$i<80;$i++) {
$random_number=floor(mt_rand(0,1000));
echo $random_number.'<br>';
$tmp=($tmp+$c+$random_number)*0.5;
$c=$b;
$b=$a;
$a=$tmp;
}


//Now to reverse
echo '<hr>';


$tmp=$a;
for ($i=0;$i<80;$i--) {
$a=$b;
$b=$c;
//$c=(unknown);
//$random_number=(($tmp-$c)-$a)*2;
//$tmp=($tmp-(($random_number*0.5)+$c+$a))*2;
//echo $random_number.'<br>';
}
?>

Your second loop is a never ending loop.

Oops I meant the following.

<?php
$a=1;
$b=47;
$c=88;
$tmp=23;
for($i=0;$i<80;$i++) {
$random_number=floor(mt_rand(0,1000));
echo $random_number.'<br>';
$tmp=($tmp+$c+$random_number)*0.5;
$c=$b;
$b=$a;
$a=$tmp;
}
 
 
//Now to reverse
echo '<hr>';
 
 
$tmp=$a;
for ($i=0;$i<80;$i++) {
$a=$b;
$b=$c;
//$c=(unknown);
//$random_number=(($tmp-$c)-$a)*2;
//$tmp=($tmp-(($random_number*0.5)+$c+$a))*2;
//echo $random_number.'<br>';
}
?>

Oops I meant the following.

<?php
$a=1;
$b=47;
$c=88;
$tmp=23;
for($i=0;$i<80;$i++) {
$random_number=floor(mt_rand(0,1000));
echo $random_number.'<br>';
$tmp=($tmp+$c+$random_number)*0.5;
$c=$b;
$b=$a;
$a=$tmp;
}
 
 
//Now to reverse
echo '<hr>';
 
 
$tmp=$a;
for ($i=0;$i<80;$i++) {
$a=$b;
$b=$c;
//$c=(unknown);
//$random_number=(($tmp-$c)-$a)*2;
//$tmp=($tmp-(($random_number*0.5)+$c+$a))*2;
//echo $random_number.'<br>';
}
?>

So did you test the new version?

So did you test the new version?

No but this is a simplified version of a bigger problem I am having with loops. I cut out everything except the main problem. So do you have an answer of how to get $c?

No but this is a simplified version of a bigger problem I am having with loops. I cut out everything except the main problem. So do you have an answer of how to get $c?

works fine for me:

<?php
$a=1;
$b=47;
$c=88;
$tmp=23;
for($i=0;$i<80;$i++) {
$random_number=floor(mt_rand(0,1000));
echo $random_number.'<br>';
$tmp=($tmp+$c+$random_number)*0.5;
$c=$b;
$b=$a;
$a=$tmp;
}


  //Now to reverse
  echo '<hr>';


    $tmp=$a;
    for ($i=0;$i<80;$i++) {
    $a=$b;
    $b=$c;
    //$c=(unknown);
    //$random_number=(($tmp-$c)-$a)*2;
    //$tmp=($tmp-(($random_number*0.5)+$c+$a))*2;
    //echo $random_number.'<br>';
    }
    echo "c=" . $c;
    ?>

and here is the result:
560
612
833
609
317
521
334
303
704
75
404
445
747
237
296
562
556
940
871
859
166
31
319
603
504
798
158
815
958
250
463
144
231
714
973
970
747
393
168
906
656
167
198
316
48
776
856
261
395
584
48
823
365
646
270
349
715
360
535
187
587
641
356
819
802
37
756
434
79
635
321
857
769
957
552
511
403
383
332
343
c=9995.29722915

Yes it is working, and also we are getting $c value init, I didn't get what we need exactly here...

<?php
$a=1;
$b=47;
$c=88;
$tmp=23;
for($i=0;$i<80;$i++) {
$random_number=floor(mt_rand(0,1000));
//echo $random_number.'<br>';
$tmp=($tmp+$c+$random_number)*0.5;
echo 'My C value Before : '.$c.'<br>';
$c=$b;
echo 'My C value After : '.$c.'<br>';
$b=$a;
$a=$tmp;
}
 
 
//Now to reverse
echo '<hr>';
 
 
$tmp=$a;
echo 'My C value Finally : '.$c.'<br>';
for ($i=0;$i<80;$i++) {
$a=$b;
$b=$c;
//$c=(unknown);
//$random_number=(($tmp-$c)-$a)*2;
//$tmp=($tmp-(($random_number*0.5)+$c+$a))*2;
//echo $random_number.'<br>';
}
?>

out put of the program is


My C value Before : 88
My C value After : 47
My C value Before : 47
My C value After : 1
My C value Before : 1
My C value After : 523
My C value Before : 523
My C value After : 505
My C value Before : 505
My C value After : 633.5
My C value Before : 633.5
My C value After : 644.25
My C value Before : 644.25
My C value After : 790.625
My C value Before : 790.625
My C value After : 1095.0625
My C value Before : 1095.0625
My C value After : 1222.65625
My C value Before : 1222.65625
My C value After : 1119.140625
My C value Before : 1119.140625
My C value After : 1244.6015625
My C value Before : 1244.6015625
My C value After : 1490.62890625
My C value Before : 1490.62890625
My C value After : 1751.384765625
My C value Before : 1751.384765625
My C value After : 1795.4931640625
My C value Before : 1795.4931640625
My C value After : 1892.5610351562
My C value Before : 1892.5610351562
My C value After : 2175.9729003906
My C value Before : 2175.9729003906
My C value After : 2272.7330322266
My C value Before : 2272.7330322266
My C value After : 2366.1470336914
My C value Before : 2366.1470336914
My C value After : 2322.559967041
My C value Before : 2322.559967041
My C value After : 2570.1464996338
My C value Before : 2570.1464996338
My C value After : 2910.6467666626
My C value Before : 2910.6467666626
My C value After : 2871.6033668518
My C value Before : 2871.6033668518
My C value After : 2920.3749332428
My C value Before : 2920.3749332428
My C value After : 2956.0108499527
My C value Before : 2956.0108499527
My C value After : 3365.3071084023
My C value Before : 3365.3071084023
My C value After : 3342.3410208225
My C value Before : 3342.3410208225
My C value After : 3194.1759353876
My C value Before : 3194.1759353876
My C value After : 3552.7415218949
My C value Before : 3552.7415218949
My C value After : 3654.0412713587
My C value Before : 3654.0412713587
My C value After : 3642.1086033732
My C value Before : 3642.1086033732
My C value After : 3631.4250626341
My C value Before : 3631.4250626341
My C value After : 3900.7331669964
My C value Before : 3900.7331669964
My C value After : 3934.9208851848
My C value Before : 3934.9208851848
My C value After : 4234.6729739094
My C value Before : 4234.6729739094
My C value After : 4315.7030704529
My C value Before : 4315.7030704529
My C value After : 4521.3119778188
My C value Before : 4521.3119778188
My C value After : 4717.4924758641
My C value Before : 4717.4924758641
My C value After : 4666.0977731585
My C value Before : 4666.0977731585
My C value After : 5036.7048754887
My C value Before : 5036.7048754887
My C value After : 5271.0986756764
My C value Before : 5271.0986756764
My C value After : 5343.5982244175
My C value Before : 5343.5982244175
My C value After : 5517.6515499531
My C value Before : 5517.6515499531
My C value After : 5643.8751128147
My C value Before : 5643.8751128147
My C value After : 5510.2366686161
My C value Before : 5510.2366686161
My C value After : 5830.9441092846
My C value Before : 5830.9441092846
My C value After : 6211.4096110497
My C value Before : 6211.4096110497
My C value After : 5955.3231398329
My C value Before : 5955.3231398329
My C value After : 5966.1336245587
My C value Before : 5966.1336245587
My C value After : 6131.7716178042
My C value Before : 6131.7716178042
My C value After : 6296.5473788185
My C value Before : 6296.5473788185
My C value After : 6138.3405016886
My C value Before : 6138.3405016886
My C value After : 6502.5560597464
My C value Before : 6502.5560597464
My C value After : 6596.0517192825
My C value Before : 6596.0517192825
My C value After : 6383.1961104856
My C value Before : 6383.1961104856
My C value After : 6903.876085116
My C value Before : 6903.876085116
My C value After : 6827.4639021992
My C value Before : 6827.4639021992
My C value After : 6937.8300063424
My C value Before : 6937.8300063424
My C value After : 7079.8530457292
My C value Before : 7079.8530457292
My C value After : 7158.6584739642
My C value Before : 7158.6584739642
My C value After : 7223.7442401533
My C value Before : 7223.7442401533
My C value After : 7424.7986429412
My C value Before : 7424.7986429412
My C value After : 7655.2285584527
My C value Before : 7655.2285584527
My C value After : 7515.486399303
My C value Before : 7515.486399303
My C value After : 7724.1425211221
My C value Before : 7724.1425211221
My C value After : 7976.6855397874
My C value Before : 7976.6855397874
My C value After : 8186.5859695452
My C value Before : 8186.5859695452
My C value After : 8139.8642453337
My C value Before : 8139.8642453337
My C value After : 8142.2748925606
My C value Before : 8142.2748925606
My C value After : 8447.4304310529
My C value Before : 8447.4304310529
My C value After : 8375.6473381933
My C value Before : 8375.6473381933
My C value After : 8280.4611153769
My C value Before : 8280.4611153769
My C value After : 8739.4457732149
My C value Before : 8739.4457732149
My C value After : 8672.0465557041
My C value Before : 8672.0465557041
My C value After : 8552.7538355405
My C value Before : 8552.7538355405
My C value After : 8804.0998043777
My C value Before : 8804.0998043777
My C value After : 8937.0731800409
My C value Before : 8937.0731800409
My C value After : 9064.9135077907
My C value Before : 9064.9135077907
My C value After : 9433.0066560842
My C value Before : 9433.0066560842
My C value After : 9593.5399180625
My C value Before : 9593.5399180625
My C value After : 9422.2267129266
My C value Finally : 9422.2267129266

commented: Very well explained +4

@saiprem - Yes that is exactly what I want my second loop to retrieve. All those values of "C value before" is what the second loop needs to retrieve. But the ultimate goal is to get all 80 values of random_number without changing the first loop. Can anybody help me there?

Are you looking like this
'C' value of first loop = 'C' value of second loop

Then try this code...

<?php
$a=1;
$b=47;
$c=88;
$tmp=23;
for($i=0;$i<80;$i++) {
$random_number=floor(mt_rand(0,1000));
//echo $random_number.'<br>';
$tmp=($tmp+$c+$random_number)*0.5;
$c=$b;
echo 'My C Value from First loop: '.$c.'<br />';
$cdata[] = $c;
$b=$a;
$a=$tmp;
}
 
 
//Now to reverse
echo '<hr>';
 
 
$tmp=$a;

for ($i=0;$i<80;$i++) {
$a=$b;
$b=$c;
echo 'My C Value from Second Loop : '.$cdata[$i].'<br />';
//$c=(unknown);
//$random_number=(($tmp-$c)-$a)*2;
//$tmp=($tmp-(($random_number*0.5)+$c+$a))*2;
//echo $random_number.'<br>';
}
?>

The only thing is I need to find C without editing the first loop. Yes it is kinda tricky but basically on the beginning of the second loop I have $a, $b, $c, $tmp from the last loop round and somehow I need to use those four variables to retrieve all 80 values of random number. And remember the $tmp contains the previous value of $tmp plus the random number and $c which is $tmp from 2 rounds ago.

Are you looking like this
'C' value of first loop = 'C' value of second loop

Then try this code...

<?php
$a=1;
$b=47;
$c=88;
$tmp=23;
for($i=0;$i<80;$i++) {
$random_number=floor(mt_rand(0,1000));
//echo $random_number.'<br>';
$tmp=($tmp+$c+$random_number)*0.5;
$c=$b;
echo 'My C Value from First loop: '.$c.'<br />';
$cdata[] = $c;
$b=$a;
$a=$tmp;
}
 
 
//Now to reverse
echo '<hr>';
 
 
$tmp=$a;

for ($i=0;$i<80;$i++) {
$a=$b;
$b=$c;
echo 'My C Value from Second Loop : '.$cdata[$i].'<br />';
//$c=(unknown);
//$random_number=(($tmp-$c)-$a)*2;
//$tmp=($tmp-(($random_number*0.5)+$c+$a))*2;
//echo $random_number.'<br>';
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.