Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Puzzle :)

Posted on 2005-03-18
26
Medium Priority
?
464 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

578 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