[SOLVED] A Challange

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mckinnon81
Forum Newbie
Posts: 12
Joined: Mon Mar 15, 2004 8:02 pm
Location: Sydney
Contact:

A Challange

Post by mckinnon81 »

Hi;

I have been given a challange that I am trying to complete using PHP Code. The challenge is:
Find a six digit number that gives its digits reversed when multiplied by an integer between 2 and 9 inclusive. For example, if the original number was 654321 and the chosen integer was 8, then 654321 x 8 should equal 123456 if it was the magic number (which of course it doesnt). There are two (2) solutions.
I have started with the following code:

Code: Select all

<?php
	for ($i = 100000;$i<=999999; $i++) &#123; // Sets out 6 digit numbers
		for ($m = 2; $m<=9; $m++)&#123; //set out multiplier integer
			$a = $i * $m; // multiplies 6 digit number by multiplier
			$r = strrev($a); //reversed answer
		&#125;
		if ($r = $i) &#123; //checks if reversed answer = 6 digit number
			echo $a;
			echo "<br>";
		&#125;
	&#125;
?>
Am I correct in my thinking of the code or am I missing something?

Any help will be appreciated.

Thanks;
Moe
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

You need to move your if statement into the for loop :)
User avatar
mckinnon81
Forum Newbie
Posts: 12
Joined: Mon Mar 15, 2004 8:02 pm
Location: Sydney
Contact:

Post by mckinnon81 »

Thanks;

I did some more tweaking of the code and I got it.

Code: Select all

<?php
   for ($i = 100000;$i<=999999; $i++) &#123; // Sets out 6 digit numbers 
     for ($m = 2; $m<=9; $m++)&#123; //set out multiplier integer 
       $a = $i * $m; // multiplies 6 digit number by multiplier 
       $r = strrev($a); //reversed answer 
         if ($r == $i) &#123; //checks if reversed answer = 6 digit number 
           echo $i."*".$m."=".$a."\n"; 
           echo "Reversed Answer: ".strrev($a)."\n";
           echo "Normal Answer: ".$i."\n\n";
         &#125; 
     &#125; 
   &#125; 
?>
Thanks for that.

I got the answers to:

109989*9=989901
Reversed Answer: 109989
Normal Answer: 109989

219978*4=879912
Reversed Answer: 219978
Normal Answer: 219978

Cheers;
Moe
Post Reply