Question

I'm are trying to teach myself to be better at programming. Part of this I have been taking puzzle that I find in newspapers and magazines and trying to find programming solutions

Today I seen a puzzle regarding numbers that are the reversed when multiplied by a number from 2-9. The example given was 1089 * 9 = 9801.

I have started to write a program in php to find the numbers this applies to and adds them to an array.

First I created a loop to cycle through the possible numbers. I then reversed each of the numbers and created a function to compare the numbered and the reversed number. The function then returns numbers that meet the criteria and adds them to an array.

This is what I have so far...

<?php

        function mul(){ // multiply number from loop

            for($i=2;$i<=9;$i++){

                $new = $num * $i;

                    if($new == $re){
                    return $new;    
                    }
                    else{
                    return;
                    }
            }
        }

        $arr =  array();

        for ($num = 1000; $num <10000; $num++) { //loop through possible numbers

                      $re =  strrev($num); // get reverse of number
                      func($re,$num); //multiply number and return correct numbers
                      $arr.push($new); //add to array??
         }
 ?>

I'm still very new to php and I find understanding programming, any pointers on a more logical way of doing this puzzle would be greatly appreciated.

Was it helpful?

Solution

Here's my solution with a nested loop. Quick and dirty.

$result = array();
for ($i = 1000; $i < 5000; $i++) {
    for ($m = 2; $m < 10; $m++) {
        if ($i*$m == (int)strrev($i)) {
            $result[] = array($i, $m);
        }
    }
}
var_dump($result);

I'd like to expand on this line:

if ($i*$m == (int) strrev($i)) {

One side is $i*$m, easy, the multipication.

On the other, we have (int)strrev($i), which means "Take $i, cast it to a string, and reverse that. Then cast it back into an int.

If that evaluates to true, an array containing $i and m is inserted into the $result array.

OTHER TIPS

I was also looking for logical questions to solve to preparing for my interview. Thanks for sharing this question.I have solved this question but using Java.I have used very basic concept.I hope you can understand and convert it to php.

public static boolean processNumber(int number)
{
    for(int i=2;i<=9;i++)
    {
        int reverseNumber=number*i;
        boolean status=checkReverse(reverseNumber,number);
        if(status)
        {
            return true;
        }
    }

    return false;
}

public static boolean checkReverse(int reverseNumber,int numberOriginal)
{
    int number=reverseNumber;
    int reverse=0,digit;
    do
    {
        digit=number%10;
        number=number/10;
        reverse=reverse*10+digit;
    }while(number>0);

    if(reverse==numberOriginal)
    {
        return true;
    }
    else
    {
        return false;
    }
}
//This is my little effort towards programming check if it satisfy your requirements 

$n = 1089;
$temp = $n;
$sum =0;
//reversing given number
while($n>1){
  $rem = $n%10;
  $sum = $sum*10 + $rem;
  $n = $n/10;
}
//checking for digit that satisfy criteria
for($i=0; $i<=9; $i++){
  if($i*$temp == $sum){
    echo "$temp * $i = $sum";
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top