Puzzle :)

Hi folks,

There haven't been enough Q's on EE since last day, so I thought that I might as well post my own to take some of your time ;-)

This is a good puzzle which I found on the Net.... how would you print a sequence of numbers 1  2  3 .... n without using any conditional operators, any other operators like >, ==, etc, no loops like for, while, do-while or any conditional statements like if-else.

(Recursion is allowed.)

- Mayank.
LVL 30
Mayank SAssociate Director - Product EngineeringAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cjjcliffordCommented:
"recursion is allowed" (when is the sequence to end?)

public class ReqSeq {
    public static void printSeq( int current, int[] ending ) {
        System.out.print( " " + current );
        int i = ending[current];
        printSeq( current + 1, ending );
    }

    public static void main( String[] args ) {
        try {  printSeq( 1, new int[10] ); } catch( Exception e ) {}
    }
}

uses index out of range to terminate recursion....
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimYatesCommented:
I'm bored...and this violates all of the rules...hehehe...

cjjclifford's answer is the best one I can think of...

Save this as Puzzle.java

/*\u002a\u002f\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0050\u0075\u007a\u007a\u006c\u0065\u007b
\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0061\u0072\u0067\u0073\u0029\u007b
\u0066\u006f\u0072\u0028\u0069\u006e\u0074\u0020\u0069\u003d\u0030\u003b\u0069\u003c\u0049\u006e\u0074\u0065\u0067\u0065\u0072\u002e\u0070\u0061\u0072\u0073\u0065\u0049\u006e\u0074\u0028\u0061\u0072\u0067\u0073\u005b\u0030\u005d\u0029\u003b
\u0069\u002b\u002b\u002c\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u0028\u0069\u002b\u0022\u0020\u0022\u0029\u0029\u003b\u007d\u007d\u002f\u002a*/

Then compile with:

javac Puzzle.java

And run with

java Puzzle 10

(where 10 is the max number)

Can you see why it works?  Hehehe a Puzzle within a Puzzle...

0
cjjcliffordCommented:
ah, unicode...
decompiled to a for() loop :-)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

durgaprasad_jCommented:
hi timyates,
 
yours is a bit different. did you convert all your pogram into hex code and dumped here ???

anyway its good, i never knew that i could write a program like this also.
0
TimYatesCommented:
>>  ah, unicode...
>> decompiled to a for() loop :-)

;-)  shhhh! ;-)

hehehe

>> yours is a bit different. did you convert all your pogram into hex code

Yeah, wrote the program, converted it to unicode chars, and added a comment round it ;-)

That way it passes the compiler, which then converts it into the proper java code just before it gets compiler ;-)

Woo!

hehehe
0
Mayank SAssociate Director - Product EngineeringAuthor Commented:
Tim, that really impressed me ;-) let me check that out :)
0
TimYatesCommented:
>> That way it passes the compiler, which then converts it into the proper java code just before it gets compiler ;-)

should read:

That way it passes the parser, which then converts it into the proper java code just before it gets compiled ;-)

hehehe
0
TimYatesCommented:
>>  Tim, that really impressed me ;-) let me check that out :)

cjjclifford's answer is the right one though ;-)
0
Mayank SAssociate Director - Product EngineeringAuthor Commented:
I know that ;-) and still don't know how your code does it (maybe it uses conditional operators ;)) he he.... lemme run it.
0
TimYatesCommented:
hee hee ;-)
0
TimYatesCommented:
compile it, then decompile it with jad ;-)
0
cjjcliffordCommented:
or you can use python:

print unicode(u'\u002a\u002f\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0050\u0075\u007a\u007a\u006c\u0065\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0061\u0072\u0067\u0073\u0029\u007b\u0066\u006f\u0072\u0028\u0069\u006e\u0074\u0020\u0069\u003d\u0030\u003b\u0069\u003c\u0049\u006e\u0074\u0065\u0067\u0065\u0072\u002e\u0070\u0061\u0072\u0073\u0065\u0049\u006e\u0074\u0028\u0061\u0072\u0067\u0073\u005b\u0030\u005d\u0029\u003b\u0069\u002b\u002b\u002c\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u0028\u0069\u002b\u0022\u0020\u0022\u0029\u0029\u003b\u007d\u007d\u002f\u002a').encode()
*/public class Puzzle{public static void main(String[] args){for(int i=0;i<Integer.parseInt(args[0]);i++,System.out.print(i+" "));}}/*

(note I stripped off the comments added to the unicode before running it through Python!)
0
TimYatesCommented:
>>  or you can use python:

neat! :-D

I keep meaning to learn Python...  especially now I have it on my phone ;-)
0
cjjcliffordCommented:
>> have it on my phone

useful (!)
0
TimYatesCommented:
Hehehe, not really, but you can access the bluetooth stack, and have sms control...

I've almost got a "Countdown numbers game" solver working too ;-)

Ahhh...  I'm such a nerd...

;-) hehehe
0
cjjcliffordCommented:
if you have access to open URLs (import urllib) you could even write a experts-exchange program :-)
0
cjjcliffordCommented:
monitoring program is what I meant, obviously :-) (Might cost you a bit on the data rates!!!)
0
TimYatesCommented:
Hee hee, yeah :-)

>> (Might cost you a bit on the data rates!!!)

That's how they get you :-(

Maybe time to work out how to route my phone's net connection via my home broadband over bluetooth...

Then the world of crazy python web parsing will be mine!!  Muhahahah!
0
dttriCommented:
 Can my Java program call another program? :-)
0
Mayank SAssociate Director - Product EngineeringAuthor Commented:
Well, this one definitely turned out to be more interesting than I had initially anticipated ;-)

>> Muhahahah!

The first time Tim didn't laugh with a he he he ;-)
0
Mayank SAssociate Director - Product EngineeringAuthor Commented:
>> Can my Java program call another program? :-)

Yes, it can. Ask it in your question and we would be glad to answer ;-)
0
TimYatesCommented:
:-D
0
dttriCommented:
 I mean the Java program call another program that print 1, 2 ... n. Isn't it a solutions? :-)
0
TimYatesCommented:
Oooh.. that's a good point! ;-)
0
cjjcliffordCommented:
you've still got to check for termination :-) (without conditionals, etc!)
0
Mayank SAssociate Director - Product EngineeringAuthor Commented:
He he.... yeah, that's a good point ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.