## nanosani 20

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.

Lets see who is the first to find out the exact numbers .... you can make a small program or you can do it MANUALLY :mrgreen: :evil:

## ALT-F4

109989-multplier is 9
219978-multiplier is 4

## nanosani 20

wow ALT F4 ... you did it .. what algo did you use for it

## ALT-F4

Nothing too fancy...

``````import java.io.*;
import java.awt.*;
import java.util.*;

public class NumberFinder {
int test;
int newNumber;

public static void main(String[] args) {
NumberFinder go = new NumberFinder();
}
public NumberFinder() {
launch();

}

public void launch() {
System.out.println("Starting to run");
for(int i=100000; i<=999999; i++){
for(int j=2; j<=9; j++){
newNumber=reverse(i);
if(i*j==newNumber){
System.out.println("The number is "+i+" and the multiplier is "+j);
}
}
}
}
public int reverse(int x){
String test = String.valueOf(x);
String done = "";
int run =5;
int sendBack;
while(run!=-1){
done = done+test.charAt(run);
run--;
}
sendBack = Integer.parseInt(done);
return sendBack;
}

}``````

## ALT-F4

I appreciate the challenge by the way! It's nice to take a shot at something other than assigned homework, you know...

## nanosani 20

nice work ... I first reversed the number by modulus .. and then started off from 499999 .... in this way the processor had to do half the work :). Your idea of reversing the number is also good.

## ALT-F4

Less operations is ALWAYS better. Do you mind posting your algorithm?? I'd like seeing a different solution.

## kirkchildress

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.

Lets see who is the first to find out the exact numbers .... you can make a small program or you can do it MANUALLY :mrgreen: :evil:[/QU1234567890

## kirkchildress

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.

Lets see who is the first to find out the exact numbers .... you can make a small program or you can do it MANUALLY :mrgreen: :evil:

1234567890

1234567890

## indignity

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.

Lets see who is the first to find out the exact numbers .... you can make a small program or you can do it MANUALLY :mrgreen: :evil:

I'd love to see more of these... that was the most enjoyable programming exercise I've had in some time...

any idea where I can find some?

## nanosani 20

I'll try to find some more of them.

## happyHour

hmm could you not do any for c? simple ones? , since like you lot im at uni and only get the standard sux ones out of the book

## dakkar

Can I add a new challenge for newbies?

swap the value of two variables, without the use of a third one.

You can use only those two variables:
a and b

:)

## Dufflepod

A standard interview question this one.

Two variables A and B. Using C notation:

A ^= B
B ^= A
A ^= B

Q.E.D.

## Narue 5,707

>Q.E.D.
Yes, but is it correct? The question clearly states "variables" yet your cute little trick will only work on integral types. What about pointers? If a and b are pointers and they point to the same object, you certainly won't get a swap out of this.

## server_crash 64

Here it is in Java notation. I don't know C.

int a;
int b;

a = a+b;

b = a-b;
a = a-b;

``````my \$digs=6;

for(\$x = 10**(\$digs-1); \$x < 10**\$digs; ++\$x){
foreach \$_ (2 .. 9) { print "\$x, \$_\n" if (\$x==reverse(\$x)/\$_ );}
}``````

A Perl solution. Outputs:

``````109989, 9
219978, 4``````

## kordaff 1

In perl, this works for non-numeric scalar variables too:

``````#!/usr/bin/perl

\$a="abcd";
\$b="yyyyyy";

\$a ^= \$b;
\$b ^= \$a;
\$a ^= \$b;

print "\$a \$b\n";``````

To swap arrays, I cannot think of a way off hand without using a counter variable to iterate through the larger array...

Kordaff

## npasma 4

Or just devote yourself to Python and use the "swap()" command! :p

I heart Python. :o

## Rashakil Fol 978

To swap Perl scalars without writing a third variable:

``(\$x, \$y) = (\$y, \$x);``

If you want to swap Perl arrays, well, you'd be better off having used array references and swapping those like above.

## Paul.Esson 38

Interesting how this has been done in two different ways, one with exclusive or operator and one with addition and subtraction.

In java we could also use, XOR in the same way.

int a, b;

a ^= b;
b ^= a;
a ^= b;

Ok, so I have a little challange now. Bit of fun.

Today we are going to swap the nibbles around in a byte
so the byte 11101010 will turn into 10101110.

can use as many varibles as you wish, but there must be a varible at the end that contains the swapped byte.

## Narue 5,707

>Today we are going to swap the nibbles around in a byte
Pfft, too easy. Do the shuffle: shift left shift right shift left OR shift right:

``````#include <stdio.h>
#include <limits.h>

#define HEXIT ( CHAR_BIT / 2 )

unsigned char swap_nibble ( unsigned char x )
{
return x << HEXIT >> HEXIT << HEXIT | x >> HEXIT;
}

int main ( void )
{
unsigned char x = 0xEA;

printf ( "Before: 0x%X\n", x );
printf ( "After:  0x%X\n", swap_nibble ( x ) );

return 0;
}``````

## Dave Sinkula 2,398

>Do the shuffle: shift left shift right shift left OR shift right:

I'm too white and can't dance as well:

``````#include <stdio.h>
#include <limits.h>

#define HEXIT ( CHAR_BIT / 2 )

unsigned char swap_nibble ( unsigned char x )
{
return x << HEXIT | x >> HEXIT;
}

int main ( void )
{
unsigned char x = 0xEA;

printf ( "Before: 0x%X\n", x );
printf ( "After:  0x%X\n", swap_nibble ( x ) );

return 0;
}

/* my output
Before: 0xEA
After:  0xAE
*/``````

## Narue 5,707

>I'm too white and can't dance as well:
It's easy, just shake it, shake it, shake it. Then shake it, shake it, shake it again:

``````#include <stdio.h>
#include <limits.h>

#define HEXIT ( CHAR_BIT / 2 )

unsigned char swap_nibble ( unsigned char x )
{
return x << HEXIT >> HEXIT << HEXIT >> HEXIT << HEXIT >> HEXIT << HEXIT
| x >> HEXIT << HEXIT >> HEXIT << HEXIT >> HEXIT << HEXIT >> HEXIT;
}

int main ( void )
{
unsigned char x = 0xEA;

printf ( "Before: 0x%X\n", x );
printf ( "After:  0x%X\n", swap_nibble ( x ) );

return 0;
}``````

## a1eio 16

just use clean python:

``````>>> a = 5
>>> b = 4
>>> a, b = b, a
>>> a
4
>>> b
5
>>> a = 'text1'
>>> b = 'text2'
>>> a, b = b, a
>>> a
'text2'
>>> b
'text1'
>>>``````

## Rashakil Fol 978

Bah, just use clean, erm, XSLT.

``````<xsl:template name="blah">
<xsl:param name="x" select="2"/>
<xsl:param name="y" select="5"/>
<xsl:param name="swapped" select="0"/>

...

<xsl:choose>
<xsl:when test="\$swapped = 0">
<!-- First we run this code. -->

<!-- And say we want to swap x and y! -->

<!-- Oh no, we can't change their values! Curse!  Recurse!!-->
<xsl:call-template name="blah">
<xsl:with-param name="x" select="\$y"/>
<xsl:with-param name="y" select="\$x"/>
<xsl:with-param name="swapped" select="1"/>
</xsl:call-template>
</xsl:when>

<!-- In the spirit of XML, they picked the most verbose name
out of the choices 'else', 'default', and 'otherwise.' -->
<xsl:otherwise>
<!-- Now x and y are swapped! -->

...
</xsl:otherwise>
</xsl:choose>
</xsl:template>``````

## numerouno

STOP! My brain is hurting!

## egoleo

yeah. its really a great challenge for us. Just look at the way guys are sending in those codes. Man i am kinda of confused now. :mrgreen:

## Daishi

Here we go. I decided to do it without using any of those dirty bitwise operators. It was fun. :o No, I don't actually code like this.

``````#include <iostream>
#include <string>

using namespace std;

string printBinary(unsigned char aByte) {  //  prints aByte in binary.
int moder=128;
string ret;
for (int i=0; i<8; i++) {
if (aByte >= moder) {
ret+='1';
aByte-=moder;
} else
ret+='0';
moder /= 2;
}
return ret;
}

unsigned char getFromBinary(string buffer) {
unsigned char ret=0, moder=1;
for (int i=buffer.length()-1; i>=0; i--) {
if ((char)buffer[i] == '1')
ret+=moder;
moder*=2;
}
return ret;
}

unsigned char swapBit(unsigned char byte, int a, int b) {
string str = printBinary(byte);
char temp = str[a];
str[a] = str[b];
str[b] = temp;

return getFromBinary(str);
}

int main(int argc, char **argv) {
unsigned char a = getFromBinary("11101010");
cout << "A is " <<  (int)a << "\t" << printBinary(a) << endl;

a = swapBit(a, 4, 5);
cout << "A is " << (int)a << "\t" << printBinary(a) << endl;

a = swapBit(a, 2, 3);
cout << "A is " << (int)a << "\t" << printBinary(a) << endl;

a = swapBit(a, 3, 4);
cout << "A is " << (int)a << "\t" << printBinary(a) << endl;

a = swapBit(a, 1, 2);
cout << "A is " << (int)a << "\t" << printBinary(a) << endl;

return 0;
}``````

It's horrible, I know.

-Fredric