Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display numbers from 1 to 100 without loops or conditions [closed]

Tags:

java

loops

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 write java.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 to PrintStream with the introduction of the Appendable 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.

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.


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\"?");

Check out the Divide + Conquer answer from the C# thread. It's evil, but brilliant:

How to print 1 to 100 without any looping using C#

Here is the Java version:

public class Application {

    public static void main(String[] args) {
        Print64Numbers();
        Print32Numbers();
        Print4Numbers();
    }

    private static int currentNumber = 0;

    private static void Print1Number() { System.out.println(++currentNumber); }
    private static void Print2Numbers() { Print1Number(); Print1Number(); }
    private static void Print4Numbers() { Print2Numbers(); Print2Numbers(); }
    private static void Print8Numbers() { Print4Numbers(); Print4Numbers(); }
    private static void Print16Numbers() { Print8Numbers(); Print8Numbers(); }
    private static void Print32Numbers() { Print16Numbers(); Print16Numbers(); }
    private static void Print64Numbers() { Print32Numbers(); Print32Numbers(); }
}

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);
}

Sure 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);