trouple with multiplying in a loop

Hello! Im new on here and new to php. Im working on this problem based on a legend involving a board of 64 squares and then 1 piece of rice on the first square two on the 2nd, 4 on the 3rd, 8 on the 4th and so on and so for for all 64. I knows its some form of loop ive tried both while and for. So stuck please help!!!
[php]

PHP Test <?php echo '

Multiplication test

'; ?> <?php for ($i=0, $k=2; $i<=64 ; $i++) { $i=$i+$i*$k; } echo "The total sum: ".$i."
"; ?> [/php]

this is the latest attempt ive tried a couple versions of a for loop and a while loop as well. the best i got so far is 64^64 but thats not correct. any help would be greatly appreciated.

Try this:
[php]

Multiplication test

<?php $pieces_of_rice = 0; // start the count at zero. for($i=0; $i<=64; $i++) { $pieces_of_rice += $i; } ?>

The total sum: <?= $pieces_of_rice; ?>

// output : 2080
[/php]

Note: In your original post you entered php just to echo out a string
[php]<?php echo '

Multiplication test

'; ?>[/php]
This is needless, you should have just used HTML. (see my modifications)

Hope that helps,
Red :wink:

Edit: I just re-read your original post and noticed you wanted
1 on the first
2 on the second
4 on the third
etc…

This script counts up in increments
1 on the first
2 on the second
3 on the third
4 on the fourth
and so on…

Red.

Here’s another snippet, I believe this one does what you want as my last one counted in increments.
[php]

<?php $pieces_of_rice = 1; // start the count at zero. $squares = range(1,64); foreach($squares as $square) { if($pieces_of_rice > 2) { if($pieces_of_rice % 2 == 1) { $pieces_of_rice -= 1; } $pieces_of_rice = $pieces_of_rice * 2; } echo 'Square No. ' . $square . ' Pieces: ' . $pieces_of_rice . '
'; $pieces_of_rice++; // increment the counter } ?>

The total sum: <?= $pieces_of_rice; ?>

// output: 9.2233720411497E+18

Square No. 1 Pieces: 1
Square No. 2 Pieces: 2
Square No. 3 Pieces: 4
Square No. 4 Pieces: 8
Square No. 5 Pieces: 16
Square No. 6 Pieces: 32
Square No. 7 Pieces: 64
Square No. 8 Pieces: 128
Square No. 9 Pieces: 256
Square No. 10 Pieces: 512
Square No. 11 Pieces: 1024
Square No. 12 Pieces: 2048
Square No. 13 Pieces: 4096
Square No. 14 Pieces: 8192
Square No. 15 Pieces: 16384
Square No. 16 Pieces: 32768
Square No. 17 Pieces: 65536
Square No. 18 Pieces: 131072
Square No. 19 Pieces: 262144
Square No. 20 Pieces: 524288
Square No. 21 Pieces: 1048576
Square No. 22 Pieces: 2097152
Square No. 23 Pieces: 4194304
Square No. 24 Pieces: 8388608
Square No. 25 Pieces: 16777216
Square No. 26 Pieces: 33554432
Square No. 27 Pieces: 67108864
Square No. 28 Pieces: 134217728
Square No. 29 Pieces: 268435456
Square No. 30 Pieces: 536870912
Square No. 31 Pieces: 1073741824
Square No. 32 Pieces: 2147483648
Square No. 33 Pieces: 4294967298
Square No. 34 Pieces: 8589934596
Square No. 35 Pieces: 17179869192
Square No. 36 Pieces: 34359738384
Square No. 37 Pieces: 68719476768
Square No. 38 Pieces: 137438953536
Square No. 39 Pieces: 274877907072
Square No. 40 Pieces: 549755814144
Square No. 41 Pieces: 1099511628288
Square No. 42 Pieces: 2199023256576
Square No. 43 Pieces: 4398046513152
Square No. 44 Pieces: 8796093026304
Square No. 45 Pieces: 17592186052608
Square No. 46 Pieces: 35184372105216
Square No. 47 Pieces: 70368744210432
Square No. 48 Pieces: 1.4073748842086E+14
Square No. 49 Pieces: 2.8147497684173E+14
Square No. 50 Pieces: 5.6294995368346E+14
Square No. 51 Pieces: 1.1258999073669E+15
Square No. 52 Pieces: 2.2517998147338E+15
Square No. 53 Pieces: 4.5035996294676E+15
Square No. 54 Pieces: 9.0071992589353E+15
Square No. 55 Pieces: 1.8014398517871E+16
Square No. 56 Pieces: 3.6028797035741E+16
Square No. 57 Pieces: 7.2057594071482E+16
Square No. 58 Pieces: 1.4411518814296E+17
Square No. 59 Pieces: 2.8823037628593E+17
Square No. 60 Pieces: 5.7646075257186E+17
Square No. 61 Pieces: 1.1529215051437E+18
Square No. 62 Pieces: 2.3058430102874E+18
Square No. 63 Pieces: 4.6116860205749E+18
Square No. 64 Pieces: 9.2233720411497E+18
[/php]

hi thanks for the reply, but thats not what im looking for. were supposed to imagine theres a board of 64 squares and starting with the first square the sequence would continue until the 64th spot: 1st spot=1,2nd=2,3rd=4,4th=8,5=16,6th=32,7=64,8=128,etc,etc to the 64th spot which end up being an insanely huge number.

Check my last post :wink:

hey thanks for the help
this is the code im using , but i dont understand exactly how it works.
[php]/find rice amount
$rice = 1;
for ($square = 1; $square <= 63; $square++)
{

$rice *= 2;}[/php]
what i dont get about this loop is where in it do $square and $rice meet? i would think there would be some statement or something that uses the two. could someone please walk me through this? I need to understand this stuff.

They don’t…
Outside the loop you have rice starting at 1.
the loop goes like this:
start at square 1, keep counting until we hit square 63, count upwards.

inside the loop, on each pass through the rice is multiplied by two and added to itself.

Make sense?

I think so …Is the multiplying and adding to itself being done with the *= ?

yes, you got it :wink:

$rice *= 2
is the shorthand version of
$rice = $rice * 2

Sponsor our Newsletter | Privacy Policy | Terms of Service