Is there a way to print numbers from 1 to 100 without using any loops or conditions like "if"?
We can easily do using recursion but that again has an if condition. Is there a way to do without using "if" as well? Also no repetitive print statements,or a single print statement containing all the numbers from 1 to 100.
A solution in Java is preferable.
views:
3751answers:
36Sure there is:
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
System.out.println(10);
System.out.println(11);
System.out.println(12);
System.out.println(13);
System.out.println(14);
System.out.println(15);
System.out.println(16);
System.out.println(17);
System.out.println(18);
System.out.println(19);
System.out.println(20);
System.out.println(21);
System.out.println(22);
System.out.println(23);
System.out.println(24);
System.out.println(25);
System.out.println(26);
System.out.println(27);
System.out.println(28);
System.out.println(29);
System.out.println(30);
System.out.println(31);
System.out.println(32);
System.out.println(33);
System.out.println(34);
System.out.println(35);
System.out.println(36);
System.out.println(37);
System.out.println(38);
System.out.println(39);
System.out.println(40);
System.out.println(41);
System.out.println(42);
System.out.println(43);
System.out.println(44);
System.out.println(45);
System.out.println(46);
System.out.println(47);
System.out.println(48);
System.out.println(49);
System.out.println(50);
System.out.println(51);
System.out.println(52);
System.out.println(53);
System.out.println(54);
System.out.println(55);
System.out.println(56);
System.out.println(57);
System.out.println(58);
System.out.println(59);
System.out.println(60);
System.out.println(61);
System.out.println(62);
System.out.println(63);
System.out.println(64);
System.out.println(65);
System.out.println(66);
System.out.println(67);
System.out.println(68);
System.out.println(69);
System.out.println(70);
System.out.println(71);
System.out.println(72);
System.out.println(73);
System.out.println(74);
System.out.println(75);
System.out.println(76);
System.out.println(77);
System.out.println(78);
System.out.println(79);
System.out.println(80);
System.out.println(81);
System.out.println(82);
System.out.println(83);
System.out.println(84);
System.out.println(85);
System.out.println(86);
System.out.println(87);
System.out.println(88);
System.out.println(89);
System.out.println(90);
System.out.println(91);
System.out.println(92);
System.out.println(93);
System.out.println(94);
System.out.println(95);
System.out.println(96);
System.out.println(97);
System.out.println(98);
System.out.println(99);
System.out.println(100);
Pseudo code. Uses an array to force an exception after 100 elements which is caught and does nothing.
function r(array a, int index){
a[index] = a[index-1]+1
print a[index]
r(a, index+1)
}
try{
array a;
a.resize(101)
r(a, 1)
}catch(OutOfBoundsException){
}
EDIT
Java code:
public void printTo100(){
int[] array = new int[101];
try{
printToArrayLimit(array, 1);
}catch(ArrayIndexOutOfBoundsException e){
}
}
public void printToArrayLimit(int[] array, int index){
array[index] = array[index-1]+1;
System.out.println(array[index]);
printToArrayLimit(array, index+1);
}
Try :
int i = 1;
System.out.println(i++);
System.out.println(i++);
System.out.println(i++);
....
Yes, it's possible, but it's terrible. There's any number of ways that will use either recursion or nested type creation, with exception handling for flow control. This has no real-world application IMO and should be avoided in real code at all costs.
Here's an example that uses recursive type instantiation with exception handling to control termination. It print the number is descending order, but that would be trivial to change to ascending, by just subtracting 99 (or whatever constant) from the value being printed.
class PrintVal
{
// called with a list containing as many items as you want, less one...
public PrintVal( List<int> items )
{
System.out.println(items.size()+1); // print the size of the list
try {
items.remove( items.size()-1 ); // will throw when items is empty
new PrintVal( items );
}
catch( Exception ) { /* swallow and terminate */ }
}
}
// setup and invocation that performs the output
ArrayList<int> strList = new ArrayList<int>( new int[99] );
PrintVal instance = new PrintVal( strList ); // all output happens here
Check out the Divide + Conquer answer from the C# thread. It's evil, but brilliant:
Implement a recursive call incrementing and printing the number. Configure your VM to run out of stack after 100 calls. No conditions, no loops. cough ;-)
without any loop and condition :
public static void recfunc(int a[], int i)
{
System.out.println(i);
int s = a[i];
recfunc(a, i + 1);
}
public static void main(String[] args)
{
int[] a = new int[100];
try
{
recfunc(a, 1);
}
catch (Exception e)
{
}
}
with recursion and without if I think use "?" for conditioning :
public static int recfunc(int i)
{
System.out.println(i);
return (i < 100) ? recfunc(i + 1) : 0;
}
public static void main(String[] args)
{
recfunc(1);
}
Abuse an exception to serve as a condition.
public class Main {
private static int[] stopper = new int[100];
public static void main(String[] args) {
try {
print(1);
} catch(ArrayIndexOutOfBoundsException e) {
// abuse of try catch
}
}
private static void print(int i) {
System.out.println(i);
stopper[i] = i;
print(i + 1);
}
}
Here's a hint that be helpful.
The assert
statement isn't the forbidden if
statement.
DO NOT DO THIS UNDER ANY SANE CIRCUMSTANCES!
public class Fail {
public void thisFails(int x){
System.out.println(x);
Integer[] bigArray = new Integer[9450];
thisFails(x+1);
}
public static void main(String[] args) {
Fail failure = new Fail();
failure.thisFails(1);
}
}
When this is ran using 1m of heap space (java -Xmx1m Fail) it will run out of heap at the 100th recursion.
...
I will now go wash my hands.
Know your libraries.
public class To100 {
public static void main(String[] args) {
String set = new java.util.BitSet() {{ set(1, 100+1); }}.toString();
System.out.append(set, 1, set.length()-1);
}
}
(You can use String.replaceAll
to change the separator. For instance, .replaceAll(", ", " ")
for space separation.)
Explanation:
java.util.BitSet
is a handy little class that represents an arbitrarily large (non-sparse) set of positive integers. (It does have so bad bits: not final, unnecessarily thread-safe, doesn't support building well, etc.)ta.- Extends
BitSet
allows me to writejava.util
only once. The JDK7 "diamond operator" should help reduce duplication with generic types, but no help for the more common situation. :( - The double braces are the Double Brace idiom - an anonymous inner class containing only an instance initialiser. It is a hack. It increase runtime size, and hence start-up time. Distribution size is negligible if you use pack200.gz. I think the modern world is ready for it. Your colleagues may not be. Perhaps start by using it for test da
BitSet.set
sets a bit in the set (two completely different meanings of the word "set" there - I like it). It's a half-open range - the top value exclusive; the bottom inclusive. Add 1 to the top to include 100.BitSet.toString
is actually precisely defined by the API docs.append
was added toPrintStream
with the introduction of theAppendable
interface in J2SE 5.0. It essentially does a substring and prints the result. (A little secret: this isn't actually guaranteed by the spec to flush the output, but implementations always will.)- Starting the append at 1, and taking one off the length strips the braces from the string representation of
BitSet
. - "Know your libraries." Taken from Josh Bloch. See Java Puzzlers, puzzle 94. It really is good to know what is in the libraries. At least know where to look. Save your time, save maintenance time and get it right first time.
Is there a way to print numbers from 1 to 100 without using any loops or conditions like "if"?
Using an optimized version of this:
System.out.println("1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100");
Next question?
Another divide and conquer:
public class Print100 {
public static void main (String...args) {
Runnable r1 = new Runnable () {
int n;
public void run () {
System.out.println(++n);
}
};
fourTimes(fiveTimes(fiveTimes(r1))).run();
}
public static Runnable twice (Runnable a) {
return add(a,a);
}
public static Runnable fourTimes (Runnable a) {
return twice(twice(a));
}
public static Runnable fiveTimes (Runnable a) {
return add(a,fourTimes(a));
}
public static Runnable add (final Runnable a, final Runnable b) {
return new Runnable () {
@Override
public void run () {
a.run();
b.run();
}
};
}
}
let Arrays do the job:
public static void main(String[] args) {
Object[] numbers = new Object[100];
Arrays.fill(numbers, new Object() {
private int count = 0;
@Override
public String toString() {
return Integer.toString(++count);
}
});
System.out.println(Arrays.toString(numbers));
}
Is there a way to print numbers from 1 to 100 without using any loops or conditions like "if"?
I can't believe noone suggested this yet:
System.out.println("numbers from 1 to 100 without using any loops or conditions like \"if\"?");
If try and catch are legal I would think it would be easy and fairly clean to recurse and then just divide by zero when you're done. Besides all that it always rocks when you get to divide by zero both for fun and profit.
public class Main {
public static void main(String[] args) {
count(100);
}
private static int count(int x) {
try {
int value=1/x;
count(x-1);
System.out.println(x);
}
catch (Exception e){
return 0;
}
return 1;
}
Here is one using a thread (I inflated the sleep time to account for fluctuations in system speed). I couldn't think of a way to get rid of the try / catch:
public class Counter extends Thread{
private int cnt;
public Counter(){
this.cnt = 0;
}
private void increment(){
System.out.println(cnt++);
try{
Thread.sleep(1000);
}catch(Exception e){}
increment();
}
public void run(){
increment();
}
public static void main(String[] args) throws Exception{
Counter cntr = new Counter();
cntr.start();
cntr.join(100000);
cntr.interrupt();
System.exit(0);
}
}
//Descending Order
class Test
{
int count = 101;
public void printNo()
{
try
{
count = count--;
int nw = count/count; // to prevent printing zero
int[] array = new int[count];
System.out.println(array.length);
printNo();
}
catch(Exception e)
{
System.out.println("DONE PRINTING");
}
}
public static void main(String[] args)
{
Test objTest = new Test();
objTest.printNo();
}
}
I'm a .Net developer but I would guess there's a Java equivalent of this...
static int i = 1;
static System.Timers.Timer timer = new System.Timers.Timer();
static void Main(string[] args)
{
timer.Interval = 10; //milliseconds
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();
//let the timer complete... (3000 to show the output stops)
System.Threading.Thread.CurrentThread.Join(3000);
}
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine(i++);
timer.Enabled = (i < 101);
}
This reminds me of programming my TI-55 years and years ago. It had 32 programmable instruction steps, and a RESET instruction that would jump to instruction zero, so simple looping could be implemented. The problem was getting it to stop, which boiled down to getting it to do an operation that caused an error, e.g., divide by zero.
Thus:
public static void main(String[] args)
{
printN(100);
}
private static void printN(int n)
{
try
{
int t = 1/n; // Exception when n is 0
printN(n-1); // Recurse, down to 0
System.out.println(n);
}
catch (Exception ex)
{
// Stop recursing
}
}
Note: Yes, I know this is similar to @Yacoby's solution.
Or if you like to use reflection :-)
public class Print100 {
public static void emit0(int index) throws Exception {
System.out.println(index);
String next = new StringBuilder()
.append("emit")
.append(index / 100)
.toString();
Print100.class.getMethod(next, Integer.TYPE)
.invoke(null, index+1);
}
public static void emit1(int index) {
}
public static void main(String[] args) throws Exception {
emit0(1);
}
}
This example uses no conditions and no exceptions.
(There is a kind of hidden condition in the short-circuit OR).
Loops are avoided by using recursion.
public class OneToHundered {
public static boolean exit() {
System.exit(0);
return true;
}
public static boolean printToHundered(int i) {
boolean r;
System.out.println(i);
r = (i<100) || exit();
printToHundered(i+1);
return r;
}
public static void main(String[] args) {
printToHundered(1);
}
}
Does it have to be Java? If ruby's allowed:
puts [*1..100].join("\n")
I'd like to see anything this concise in Java.
My solution without verbosity. It doesn't use any control structure other than function application. It also doesn't use library code to help out. My code is easily extensible to print out the range [a, b]. Just change conts [n / 100]
to conts [(n - a) / (b - a)]
and of course change new Printable (1)
to new Printable (a)
.
To100.java:
class Printable {
private static final Continuation[] conts = {new Next (), new Stop ()};
private final int n;
private final Continuation cont;
Printable (int n) {
this.n = n;
this.cont = conts [n / 100];
}
public void print () {
System.out.println (n);
cont.call (n);
}
}
interface Continuation {
public void call (int n);
}
class Next implements Continuation {
public void call (int n) {
new Printable (n + 1).print ();
}
}
class Stop implements Continuation {
public void call (int n) {
// intentionally empty
}
}
class To100 {
public static void main (String[] args) {
new Printable (1).print ();
}
}
EDIT: Since this question was closed (why???) I'll post my second answer here. It is inspired by Tom Hawtin's notice that the program doesn't have to terminate. Also the question doesn't require that only the numbers 1-100 are printed (or even in order).
To100Again.java:
class To100Again extends Thread {
private static byte n;
public void run () {
System.out.println (n++);
new To100Again ().start ();
System.gc();
}
public static void main (String[] args) {
new To100Again ().start ();
}
}
Building on Yacoby's answer, but without the catch. (Upvote that answer.)
public class To100 {
public static void main(String[] args) {
final int max = 100;
new java.util.concurrent.Semaphore(max) {
void go() {
acquireUninterruptibly();
System.err.println(max-availablePermits());
go();
}
}.go();
}
}
Explanation:
Semaphore
allows a specified number of permits to be acquired before blocking.- I didn't want to write
java.util.concurrent
twice, so I opportunistically extendedSemaphore
. - This uses an anonymous inner class. Anonymous does not mean it is not a type. I can therefore call a method on it that is not declared in a base type/implemented interface.
acquireUninterruptibly
means I don't have to declare pesky checked exceptions.- Nobody said the program had to terminate.
And then God invented Perl (larry wall actually invented it.. :-)
#!/usr/bin/perl
@range = 1..100;
print @range;
In C++:
#include <iostream>
class a {
static unsigned i;
public:
a() {
std::cout << ++i << std::endl;
}
};
unsigned a::i = 0U;
int main() {
a array[100];
}
This solution neither uses loops nor recursion for printing numbers from 1 to 100.
No conditions (no short-cut boolean operators, no ?-operator, no exceptions), no loops:
import java.util.Vector;
public class PrintOneToHundered {
static int i;
PrintOneToHundered() {}
public String toString() { return ++i+""; }
public static void main(String[] args) {
Vector v1 =new Vector(); v1 .add(new PrintOneToHundered());
Vector v2 =new Vector(); v2 .addAll(v1 ); v2 .addAll(v1 );
Vector v4 =new Vector(); v4 .addAll(v2 ); v4 .addAll(v2 );
Vector v8 =new Vector(); v8 .addAll(v4 ); v8 .addAll(v4 );
Vector v16 =new Vector(); v16 .addAll(v8 ); v16 .addAll(v8 );
Vector v32 =new Vector(); v32 .addAll(v16); v32 .addAll(v16);
Vector v64 =new Vector(); v64 .addAll(v32); v64 .addAll(v32);
Vector v100=new Vector(); v100.addAll(v64); v100.addAll(v32); v100.addAll(v4);
System.out.println(v100);
}
}
Explanation:
- define a class, whose toString-method returns consecutive ints at repeated calls
- create a vector with 100 elements, that are instances of the class
- print the vector (toString-method of a Vector returns a string of the toString-values of all its elements)
i would think there would be a java equivalent to something like the following php
$my_numbers = range(0,100);
echo implode($my_numbers, ' ');
this avoids recursion, loops, control statements, etc.
Ok, I'm late on this and an answer is already accepted, but I wonder why nobody has used a clean and simple counter yet?
public class Counter
{
static Counter[] vtab = new Counter[]
{
new Counter(),
new Counter() { public void print( int first, int last ) {} }
};
public void print( int first, int last )
{
vtab[ ( last - first - 1 ) >>> 31 ].print( first, last - 1 );
System.out.println( last );
}
public static void main( String[] args )
{
vtab[ 0 ].print( 1, 100 );
}
}
Thread safe, configurable, no exceptions, no dependance on API side effects, just plain OOP and some trivial math.
For those not familiar with binary operators here is how it works:
The
( x >>> n )
expression moves all bits of the integer valuex
to the right byn
places. Lower bits simply fall off the right side by this operation and new bits that come in from the left side are always0
.So the effect of
( x >>> 31 )
is to move the highest bit ofx
to the lowest place and to set all other bits ofx
to0
. The result is now always either0
or1
for all possible values ofx
.As the highest bit of an
int
is the sign bit which is0
for positive values and1
for negative values, the expression( x >>> 31 )
evaluates to0
for all positve values ofx
and to1
for all negative values ofx
.Now if both
first
andlast
are positive numbers and iflast
is greater thanfirst
, the result of( last - first - 1 )
will be>= 0
and iflast == first
it will be-1
.So
( ( last - first - 1 ) >>> 31 )
evaluates to0
iflast
is greater thanfirst
and becomes1
if they are equal.
Now this value 0/1
is used to switch between the 2 implementations of print( int first, int last )
based on the comparision of first
and last
. At first the recursion takes place without printing anything. print( 1, 100 )
calls print( 1, 99 )
and so on... until last
equals first
which causes a switch to the other implementation of print
which in turn does nothing at all. So now the stack unwinds again and the values are printed on the way down in ascending order and the invocation of vtab[ 0 ].print( 1, 100 )
finishes normally.
Didn't see this in here, using the termination order of the &&
operator.
public class count100 {
public static boolean loop(int i) {
System.out.println(100-i);
return i > 0 && loop(i-1);
}
public static void main(String[] args) {
loop(99);
}
}
This answer is perverse enough that it doesn't even look like it will run. ;)
It gets extra text at the end of the output, but it avoid loops, conditions, main() and println(). ;)
public class OneHundred {
private static int i = 1;
static {
OneHundred[] hundreds = new OneHundred[100];
Arrays.fill(hundreds, new OneHundred(););
Thread.currentThread().setName(Arrays.toString(hundreds).replaceAll("[\\]\\[, ]+", "\n"));
clear("Exception in thread \""); clear("\" ");
}
private static void clear(String string) {
try {
Field f = String.class.getDeclaredField("count");
f.setAccessible(true);
f.set(string, 0);
} catch (Exception ignored) { }
}
public String toString() { return "" + i++; }
}
You don't need array's... just recursion... Try this...
public class TestFile {
/** * @param args */ public static void main(String[] args)throws Exception { TestFile f = new TestFile(); f.print(100); }
public void print(int x){ if(x>0){ System.out.println(x); x--; print(x); } } }