?
Solved

Puzzle :)

Posted on 2005-03-18
26
Medium Priority
?
461 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:Mayank S
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 7
  • 5
  • +2
26 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 200 total points
ID: 13572701
"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
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 200 total points
ID: 13572928
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
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13572993
ah, unicode...
decompiled to a for() loop :-)
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13573021
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13573049
>>  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
 
LVL 30

Author Comment

by:Mayank S
ID: 13573092
Tim, that really impressed me ;-) let me check that out :)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13573109
>> 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
 
LVL 35

Expert Comment

by:TimYates
ID: 13573110
>>  Tim, that really impressed me ;-) let me check that out :)

cjjclifford's answer is the right one though ;-)
0
 
LVL 30

Author Comment

by:Mayank S
ID: 13573134
I know that ;-) and still don't know how your code does it (maybe it uses conditional operators ;)) he he.... lemme run it.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13573163
hee hee ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13573165
compile it, then decompile it with jad ;-)
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13573540
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13573658
>>  or you can use python:

neat! :-D

I keep meaning to learn Python...  especially now I have it on my phone ;-)
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13573686
>> have it on my phone

useful (!)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13573710
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
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13573752
if you have access to open URLs (import urllib) you could even write a experts-exchange program :-)
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13573756
monitoring program is what I meant, obviously :-) (Might cost you a bit on the data rates!!!)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13573790
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
 
LVL 7

Expert Comment

by:dttri
ID: 13576118
 Can my Java program call another program? :-)
0
 
LVL 30

Author Comment

by:Mayank S
ID: 13588700
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
 
LVL 30

Author Comment

by:Mayank S
ID: 13588708
>> Can my Java program call another program? :-)

Yes, it can. Ask it in your question and we would be glad to answer ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13589479
:-D
0
 
LVL 7

Expert Comment

by:dttri
ID: 13591750
 I mean the Java program call another program that print 1, 2 ... n. Isn't it a solutions? :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13591814
Oooh.. that's a good point! ;-)
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13591879
you've still got to check for termination :-) (without conditionals, etc!)
0
 
LVL 30

Author Comment

by:Mayank S
ID: 13598475
He he.... yeah, that's a good point ;-)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question