954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A challenge for all newbies

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:

nanosani
Unauthenticated Liar
Team Colleague
1,830 posts since Jul 2004
Reputation Points: 45
Solved Threads: 56
 

109989-multplier is 9
219978-multiplier is 4

ALT-F4
Newbie Poster
12 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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

nanosani
Unauthenticated Liar
Team Colleague
1,830 posts since Jul 2004
Reputation Points: 45
Solved Threads: 56
 

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
Newbie Poster
12 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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

ALT-F4
Newbie Poster
12 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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.

nanosani
Unauthenticated Liar
Team Colleague
1,830 posts since Jul 2004
Reputation Points: 45
Solved Threads: 56
 

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

ALT-F4
Newbie Poster
12 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

[QUOTE=nanosani]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
Newbie Poster
3 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

[QUOTE=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

kirkchildress
Newbie Poster
3 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

1234567890

kirkchildress
Newbie Poster
3 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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?

indignity
Newbie Poster
5 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

I'll try to find some more of them.

nanosani
Unauthenticated Liar
Team Colleague
1,830 posts since Jul 2004
Reputation Points: 45
Solved Threads: 56
 

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

happyHour
Newbie Poster
12 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

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

:)

dakkar
Newbie Poster
14 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

A standard interview question this one.

Two variables A and B. Using C notation:

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

Q.E.D.

Dufflepod
Newbie Poster
1 post since May 2005
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

int a;
int b;

a = a+b;


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

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 
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
Amadaeus09
Newbie Poster
1 post since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

kordaff
Newbie Poster
20 posts since Jun 2005
Reputation Points: 11
Solved Threads: 0
 

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

I heart Python. :o

npasma
Light Poster
41 posts since Jun 2005
Reputation Points: 14
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You