Re: [PHP] help with script needed
Richard Lynch wrote: You could also just use: if (($i % 15) === 0) echo FooBar; elseif (($i % 3) === 0) echo Foo; elseif (($i % 5) === 0) echo Bar; 15 works because 3 and 5 are mutually prime or whatever it's called. Good point, missed that one. A minimalist might not even bother with the 15 and would skip the 'else' and just do: if (($i % 3) === 0) echo Foo; if (($i % 5) === 0) echo Bar; echo \n; I personally think that's less comprehensible, however, and less maintainable, so I'd rather see a couple more CPU cycles than have code I have to think about to figure out what it does, until it's a proven bottleneck. Except that you're not displaying the actual number when it's not divisible between 3 or 5, so that doesn't meet the spec. Sorry. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
You could also just use: if (($i % 15) === 0) echo FooBar; elseif (($i % 3) === 0) echo Foo; elseif (($i % 5) === 0) echo Bar; 15 works because 3 and 5 are mutually prime or whatever it's called. The order matters, though, as moving the 15 after the other if tests would fail, as 3 and/or 5 would kick in first. It's also a game played in grade school when learning clock arithmetic (aka the modulus operator) at least here in the Midwest, usually around 4th grade (age 10), as I recall. Each person in turn says the next number or Fizz or Buzz or FizzZBuzz, and if somebody messes up, you razz them for it, and start over at 1. The person who makes the mistake could also be out in a musical chairs sort of thing, though that risks boredom creeping in for a large group of people no longer actively playing. You do not want a roomful of bored 4th-graders. Trust me on that one. :-) If the numbers are not mutually prime, you would have to do an test as Stut posted. A minimalist might not even bother with the 15 and would skip the 'else' and just do: if (($i % 3) === 0) echo Foo; if (($i % 5) === 0) echo Bar; echo \n; I personally think that's less comprehensible, however, and less maintainable, so I'd rather see a couple more CPU cycles than have code I have to think about to figure out what it does, until it's a proven bottleneck. YMMV On Wed, March 7, 2007 4:00 pm, Bruce Gilbert wrote: Thanks for the responses so far. This is what I have come up with [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; if ($i%3 == 0) echo Foo ; elseif ($i%5 == 0) echo Bar; } ? [/php] and the results can be seen here http://www.inspired-evolution.com/image_test/numbers_output.php I actually want the Foo and Bar to replace the numbers they are dereivatives of and not appear below as they are now, and I also need to add a third condition for FooBar which would be for numbers that are both divisible by 3 and 5. thanks On 3/7/07, Martin Marques martin@bugs.unl.edu.ar wrote: Tijnema ! escribió: On 3/7/07, Bruce Gilbert [EMAIL PROTECTED] wrote: I just need to add code to print something different, say foo if the output is a multiple of 5 or 10 for example. How do I go about doing this? I've seen that question a lot, what i use is fairly simple if( intval($number / $multiple) == ($number / $multiple ) ) try that Very bad solution. Try the % operator: if($number % $multiple == 0) http://www.php.net/manual/en/language.operators.arithmetic.php -- select 'mmarques' || '@' || 'unl.edu.ar' AS email; - Martín Marqués | Programador, DBA Centro de Telemática| Administrador Universidad Nacional del Litoral - -- ::Bruce:: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Some people have a gift link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
Stut wrote: Bruce Gilbert wrote: Thanks for the responses so far. This is what I have come up with [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; if ($i%3 == 0) echo Foo ; elseif ($i%5 == 0) echo Bar; } ? [/php] and the results can be seen here http://www.inspired-evolution.com/image_test/numbers_output.php I actually want the Foo and Bar to replace the numbers they are dereivatives of and not appear below as they are now, and I also need to add a third condition for FooBar which would be for numbers that are both divisible by 3 and 5. Ok, this is commonly known as the FizzBuzz problem and is used a lot as a university project or interview question. If you can't do it, be afraid!! http://dev.stut.net/php/fizzbuzz.php $upto = 100; $cur = 1; for ($cur = 1; $cur = $upto; $cur++) { if ($cur % 3 == 0) { print 'Fizz'; if ($cur % 5 == 0) print 'Buzz'; } else if ($cur % 5 == 0) { print 'Buzz'; } else { print $cur; } print br /; } no need to do each modulo twice per iteration :-) -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] help with script needed
I have a little script that prints a number out from 1 to 100 [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; } ? [/php] I just need to add code to print something different, say foo if the output is a multiple of 5 or 10 for example. How do I go about doing this? -- ::Bruce:: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
On 3/7/07, Bruce Gilbert [EMAIL PROTECTED] wrote: I have a little script that prints a number out from 1 to 100 [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; } ? [/php] I just need to add code to print something different, say foo if the output is a multiple of 5 or 10 for example. How do I go about doing this? -- ::Bruce:: I've seen that question a lot, what i use is fairly simple if( intval($number / $multiple) == ($number / $multiple ) ) try that Tijnema -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
try this if ($i%5 == 0) echo foobr; -afan I have a little script that prints a number out from 1 to 100 [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; } ? [/php] I just need to add code to print something different, say foo if the output is a multiple of 5 or 10 for example. How do I go about doing this? -- ::Bruce:: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
oops, ofcourse whe have the modular :) On 3/7/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: try this if ($i%5 == 0) echo foobr; -afan I have a little script that prints a number out from 1 to 100 [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; } ? [/php] I just need to add code to print something different, say foo if the output is a multiple of 5 or 10 for example. How do I go about doing this? -- ::Bruce:: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
Tijnema ! escribió: On 3/7/07, Bruce Gilbert [EMAIL PROTECTED] wrote: I just need to add code to print something different, say foo if the output is a multiple of 5 or 10 for example. How do I go about doing this? I've seen that question a lot, what i use is fairly simple if( intval($number / $multiple) == ($number / $multiple ) ) try that Very bad solution. Try the % operator: if($number % $multiple == 0) http://www.php.net/manual/en/language.operators.arithmetic.php -- select 'mmarques' || '@' || 'unl.edu.ar' AS email; - Martín Marqués | Programador, DBA Centro de Telemática| Administrador Universidad Nacional del Litoral - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
Thanks for the responses so far. This is what I have come up with [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; if ($i%3 == 0) echo Foo ; elseif ($i%5 == 0) echo Bar; } ? [/php] and the results can be seen here http://www.inspired-evolution.com/image_test/numbers_output.php I actually want the Foo and Bar to replace the numbers they are dereivatives of and not appear below as they are now, and I also need to add a third condition for FooBar which would be for numbers that are both divisible by 3 and 5. thanks On 3/7/07, Martin Marques martin@bugs.unl.edu.ar wrote: Tijnema ! escribió: On 3/7/07, Bruce Gilbert [EMAIL PROTECTED] wrote: I just need to add code to print something different, say foo if the output is a multiple of 5 or 10 for example. How do I go about doing this? I've seen that question a lot, what i use is fairly simple if( intval($number / $multiple) == ($number / $multiple ) ) try that Very bad solution. Try the % operator: if($number % $multiple == 0) http://www.php.net/manual/en/language.operators.arithmetic.php -- select 'mmarques' || '@' || 'unl.edu.ar' AS email; - Martín Marqués | Programador, DBA Centro de Telemática| Administrador Universidad Nacional del Litoral - -- ::Bruce:: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
Are you only using 3 and 5? just echo 3 and 5 instead of Foo and Bar. and for both, just have a 3rd condition including both ... if (($i%3 ==0) ($i%5 == 0)) echo both or foobar.. does this help? Jake - Original Message - From: Bruce Gilbert [EMAIL PROTECTED] To: Martin Marques martin@bugs.unl.edu.ar Cc: Tijnema ! [EMAIL PROTECTED]; PHP-General php-general@lists.php.net Sent: Wednesday, March 07, 2007 4:00 PM Subject: Re: [PHP] help with script needed Thanks for the responses so far. This is what I have come up with [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; if ($i%3 == 0) echo Foo ; elseif ($i%5 == 0) echo Bar; } ? [/php] and the results can be seen here http://www.inspired-evolution.com/image_test/numbers_output.php I actually want the Foo and Bar to replace the numbers they are dereivatives of and not appear below as they are now, and I also need to add a third condition for FooBar which would be for numbers that are both divisible by 3 and 5. thanks On 3/7/07, Martin Marques martin@bugs.unl.edu.ar wrote: Tijnema ! escribió: On 3/7/07, Bruce Gilbert [EMAIL PROTECTED] wrote: I just need to add code to print something different, say foo if the output is a multiple of 5 or 10 for example. How do I go about doing this? I've seen that question a lot, what i use is fairly simple if( intval($number / $multiple) == ($number / $multiple ) ) try that Very bad solution. Try the % operator: if($number % $multiple == 0) http://www.php.net/manual/en/language.operators.arithmetic.php -- select 'mmarques' || '@' || 'unl.edu.ar' AS email; - Martín Marqués | Programador, DBA Centro de Telemática| Administrador Universidad Nacional del Litoral - -- ::Bruce:: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
Bruce Gilbert wrote: Thanks for the responses so far. This is what I have come up with [php] ?php for( $i=1; $i=100; $i++ ) { echo $i; echo br; if ($i%3 == 0) echo Foo ; elseif ($i%5 == 0) echo Bar; } ? [/php] and the results can be seen here http://www.inspired-evolution.com/image_test/numbers_output.php I actually want the Foo and Bar to replace the numbers they are dereivatives of and not appear below as they are now, and I also need to add a third condition for FooBar which would be for numbers that are both divisible by 3 and 5. Ok, this is commonly known as the FizzBuzz problem and is used a lot as a university project or interview question. If you can't do it, be afraid!! http://dev.stut.net/php/fizzbuzz.php -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with script needed
LOL I told'ya I rememberd it from my 2nd semester :) Ok, this is commonly known as the FizzBuzz problem and is used a lot as a university project or interview question. If you can't do it, be afraid!! http://dev.stut.net/php/fizzbuzz.php -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php