6

Maybe first idea come to mind to solve this kind of problems is recursive function but also it will be a challenge to write recursive function without any condition.

I tried this approach to print numbers from 10 to 60:

public static void printNumbers(int n){
       int divisonByZero = 1 / (61 - n);
       System.out.println(n);
       printNumbers(n+1);
}     
public static void main(String[] args) {
       printNumbers(10);
}   

But it will crash when it reach number 61 without exception handling

also even with try catch the Arithmetic Exception it's still not a preferable solution because it's handling an exception (runtime error).

I think main problem when using recursive functions is the stop condition.

As well I read that there is a way in C++ by creating a class with a static variable counter and initialize it then increment the counter variable and print it in the constructor after that instantiating number of objects of class counter will print these numbers.

Any suggested solutions to solve this challenge would be appreciated.

Oghli
  • 2,200
  • 1
  • 15
  • 37
  • 6
    Are you allowed to use the `range` method of the `IntStream` class? – Dawood ibn Kareem Jun 06 '17 at 20:22
  • 2
    `System.out.printLine("10,11,12,13,14,15...60");`? If the range is determined before you write the code, this works at least. – E.D. Jun 06 '17 at 20:24
  • Btw, as @DawoodibnKareem said if you are able to use `IntStream` you can do the following in Java8. `IntStream.range(1, 10).forEach(System.out::println);` But, it uses `forEach`. – Isabek Tashiev Jun 06 '17 at 20:28
  • @DawoodibnKareem I can't use range method of `IntStream` – Oghli Jun 06 '17 at 20:31
  • @IsaBek `forEach` isn't allowed. – Oghli Jun 06 '17 at 20:32
  • It's a method call, not a for each loop. @Colt – Dawood ibn Kareem Jun 06 '17 at 20:32
  • 3
    Two threads. The first thread uses recursion to put them in an array, and a % operator to make sure they don't overspill the array. The other thread sleeps for a bit, prints the array, then kills the first thread. – Dawood ibn Kareem Jun 06 '17 at 20:38
  • 2
    You can do it with lambda. Turn everything into lambda calculus and solve it that way. your conditions and loops will be anonymous function calls. See [this video](https://www.youtube.com/watch?v=VUhlNx_-wYk) for inspiration. – Sylwester Jun 06 '17 at 20:52
  • @DawoodibnKareem we should implement it to see if threads may solve it. – Oghli Jun 06 '17 at 21:01
  • OK, good luck. Make sure you throttle the first thread sufficiently that you don't get stack overflow before the second thread kills it. – Dawood ibn Kareem Jun 06 '17 at 21:04
  • If you want to avoid conditions completely this seems impossible. Any solution would contain a condition hidden away somewhere. – Thijs Steel Jun 06 '17 at 21:18
  • @ThijsSteel Yeah it's challenging but not impossible to solve. – Oghli Jun 06 '17 at 21:23
  • @DawoodibnKareem how do you print the array without a loop, even a library call to print an array will use a loop. – Imposter Jun 11 '17 at 03:23
  • I figure if I'm allowed to call `System.out.println`, I must be allowed to call `Arrays.toString`. @Imposter – Dawood ibn Kareem Jun 11 '17 at 03:45
  • Are you allowed to use switch-case or it is considered as a condition and is not allowed? If you are allowed I suggest to implement an automata. – hadi.mansouri Jun 14 '17 at 13:05

16 Answers16

6

You can do something like this: (Idea taken from this answer)

public class Application {

    public static void main(String[] args) {
        Print40Numbers();
        Print10Numbers();

    }

    private static int currentNumber = 10;

    private static void Print1Number() { System.out.println(currentNumber++); }
    private static void Print2Numbers() { Print1Number(); Print1Number(); }    
    private static void Print5Numbers() { Print2Numbers(); Print2Numbers(); Print1Number(); }   
    private static void Print10Numbers() { Print5Numbers();Print5Numbers();}
    private static void Print20Numbers() { Print10Numbers();Print10Numbers();}
    private static void Print40Numbers() { Print20Numbers();Print20Numbers();}



}
Santiago Salem
  • 577
  • 2
  • 12
  • Nice idea. it's much more like implementing recursive algorithm in a manual approach without needing to put a stop condition as in recursive function. – Oghli Jun 06 '17 at 21:41
  • @Colt it's a static solution, you have to recompile the whole thing if you want to adjust the interval. In this case it's easier just to hard-code every value you want to print and be done with it. – D. Kovács Jun 15 '17 at 12:33
  • I sense the smell of Prolog and the similar declaratives here. :) – Franta Jun 26 '17 at 16:09
5

Based on @Imposter's answer, a reduce version with compacted but readable code

class Sandbox
{
    public static void main(String args[]) throws Exception
    {
        System.out.println(getfalse(10, 60));
    }

    public static String getfalse(Integer start, Integer stop) throws Exception
    {
        return
            start + "\n" +
            Sandbox.class.getMethod("get" + (start == stop), Integer.class, Integer.class)
            .invoke(Sandbox.class, start+1, stop);
    }

    public static String gettrue(Integer start, Integer stop)
    {
        return "";
    }
}
ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • Thanks.can you explain your way and how `getfalse()` and `gettrue()` methods work. – Oghli Jun 13 '17 at 21:41
  • 1
    `getfalse` is invoked when `stop` is not yet reached, it concatenates the current number and calls `gettrue` if it was the last number to deal with (`start` == `stop`) or calls `getfalse` if `stop` is not yet reached (`start` != `stop`) etc... – ToYonos Jun 14 '17 at 08:43
  • `start` == `stop` → conditional → does not answer the question – D. Kovács Jun 15 '17 at 06:54
  • I do not agree, it's a boolean expression but it's all about interpretation here – ToYonos Jun 15 '17 at 13:26
  • class.getMethod uses conditionals – Kyle Berezin Jun 16 '17 at 03:18
  • 1
    ToYonos Thanks alot. your solution worked perfect and it's clean and elegant.it deserves the bounty. also thank you @Imposter for the idea to this solution. – Oghli Jun 16 '17 at 03:26
  • @Colt Well "nice and clean" can't be said to any of the answers given, simply because the requirements were weird. I just entered two different solutions to this question, which both are just slightly ironic ;) In a production environment the best way to do this would be `IntStream.range(10, 60).forEach(System.out::println);` without a doubt. – Felix Jun 16 '17 at 04:14
  • 2
    I don't agree with this solution, the `==` is essentially an `if` (the generated bytecode is the same `ifne` as in a standard `if` statement). – D. Kovács Jun 16 '17 at 06:51
  • @rollback first time I ask the question @Dawood ibn Kareem suggest in the comment `IntStream.range` solution and i replied to him that it's not allowed to use it in this problem reqs. Actually `.forEach()` is a new method in java 8 and I am not allowed to use it because it's considered a for loop which implemented in different way. read the documentation to be sure of that .forEach() It is defined in Iterable and Stream interface. It is a default method defined in the Iterable interface. Collection classes which extends Iterable interface can use forEach loop to iterate elements. – Oghli Jun 16 '17 at 10:52
  • link to the documentation of java 8 features: https://www.javatpoint.com/java-8-foreach – Oghli Jun 16 '17 at 10:53
  • Ugh, this definitely is not "clean": `get" + (start == stop` Here, the **Boolean literals** "true" and "false" are **transformed into Strings**! There is nothing clean, oppositely, this is a **dirty hack**! – Franta Jun 26 '17 at 16:13
4

Here is a solution using a hashmap, a bitwise operator, an equality expression and reflection:

import java.lang.reflect.*;
import java.util.*;

public class Range
{

  private final Map<Boolean, Integer> truth;

  Range()
  {
    truth = new HashMap<>();
    truth.put(true, 0);
    truth.put(false, 1);
  }

  public void printRange(int start, int stop) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
  {
    print1(start, stop);
  }

  public void print1(Integer start, Integer stop) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
  {
    int quit = start ^ stop;
    int method = truth.get(quit == 0);
    System.out.println(start);

    String whichMethod = Integer.toString(method);
    Method toCall = this.getClass().getMethod("print" + whichMethod, Integer.class, Integer.class);
    toCall.invoke(this, start + 1, stop);
  }

  public void print0(Integer start, Integer stop)
  {
    System.exit(0);
  }

  public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
  {
    Range range = new Range();
    range.printRange(-10, 60);
  }
}

Ok so that is one way to do it and here is the more OO way where you don't use the extra stuff.

import java.util.*;

public class Range
{

  interface Printer
  {
    void print(int start, int end);
  }

  private final Map<Boolean, Printer> truth;

  Range()
  {
    truth = new HashMap<>();
    truth.put(true, new Quit());
    truth.put(false, new KeepGoing());
  }

  public void printRange(int start, int stop)
  {
    truth.get(false).print(start, stop);
  }

  private class KeepGoing implements Printer
  {
    public void print(int start, int stop)
    {
      int quit = start ^ stop;
      Printer method = truth.get(quit == 0);
      System.out.println(start);

      method.print(start + 1, stop);
    }
  }

  private class Quit implements Printer
  {
    public void print(int start, int stop)
    {
      return;
    }
  }

  public static void main(String[] args)
  {
    Range range = new Range();
    range.printRange(-10, 60);
  }
}
Imposter
  • 253
  • 1
  • 10
  • Thanks! can you explain your first approach in more detail so i can understand it well. I Think you are the only one who may solve this challenge. – Oghli Jun 13 '17 at 21:46
  • 1
    @Colt The first approach uses [reflection](https://docs.oracle.com/javase/tutorial/reflect/). The link is the java trail for reflection. Check it out. Basically I am using a string to identify which method in this class to call. The value side of the HashMap is the arbiter of sorts. By appending the name of the method, the decision is made. When its time to quit, I use `System.exit` and yank the carpet out from under the jvm. The second solution IMHO is much preferred wrt the first. – Imposter Jun 14 '17 at 00:04
  • Just a note that the XOR, `^` is not needed in either implementation. The equality expression could just be `start == stop`, and remove the `quit` variable all together. – Imposter Jun 14 '17 at 00:11
  • 1
    `==` → conditional → does not answer the question – D. Kovács Jun 15 '17 at 06:55
  • 2
    @D.Kovács `==` is not a conditional. It is an equality expression. Check the [JLS](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.21). The result of an equality expression is a boolean. – Imposter Jun 16 '17 at 00:36
  • @D.Kovács is right, java can only use booleans in conditionals. if you look at public V get(Object var1) in HashMap.class you will see a lambda expression. – Kyle Berezin Jun 16 '17 at 02:25
4

You can use semaphores to limit the recursion count:

import java.util.concurrent.Semaphore;

public class PrintNumbers extends Thread 
{
  static int start = 10;
  static int end = 60;

  static Semaphore available = new Semaphore(end - start, true);
  static Semaphore completed = new Semaphore(end - start, true);

  public static void main(String[] args) throws Exception {
    completed.drainPermits();  
    (new PrintNumbers()).start(); //Start watcher thread
    counter(start);
  }

  // Recursive function for counting
  public static void counter(int count) throws Exception{
    System.out.println(count);
    count++;
    completed.release();
    available.acquire(); // Will stop here when there is no more to count
    counter(count);
  }  

  public void run() {
    completed.acquireUninterruptibly(end - start);
    System.exit(0);  // Terminate process
  }
}

PrintNumbers class launches a watcher thread to terminate the process after counting is complete.

  • internally uses conditionals → not an answer to the question – D. Kovács Jun 15 '17 at 06:48
  • Could you be more specific? Which statement uses a conditional internally? – OmerKocaoglu Jun 15 '17 at 20:19
  • 1
    If you are concerned about the if statements inside the Semaphore class, the System.out.println function alone contains several conditionals as well. Then you would not be able to print a single number to the console without an internal conditional. I think the criteria here is about avoiding explicit use of conditionals. – OmerKocaoglu Jun 15 '17 at 22:15
  • Great solution! thank you. can you explain how it works. – Oghli Jun 16 '17 at 03:21
  • @OmerKocaoglu I don't agree with you: if you don't care about internal `if`-s, then an `IntStream` is as good as the solutions with reflection or exceptions or sempahores. – D. Kovács Jun 16 '17 at 06:49
  • @D.Kovács I agree with OmerKocaoglu. as he said "the criteria here is about avoiding explicit use of conditionals." also Dawood ibn Kareem suggest Threads solution in the comments. – Oghli Jun 16 '17 at 11:08
  • "the criteria here is about avoiding explicit use of conditionals." → then use an `IntStream`! :) – D. Kovács Jun 16 '17 at 11:38
  • Thanks. A semaphore is basically a set of locks that can be used for coordinating consumption of limited resources. In this example, these locks are used for limiting the recursions. Each recursion allocates a lock from the first semaphore, "available" and releases one from the second semaphore "completed". The main thread enters wait state when all locks in the "available" semaphore are consumed. – OmerKocaoglu Jun 16 '17 at 18:01
  • The second thread waits for the moment when all locks in the second semaphore, "completed" become available. This is made possible by the recursions on the main thread, each releasing a single lock on the "completed" semaphore. When the second semaphore is fully available, the only thing left for the watcher thread is to terminate the process. – OmerKocaoglu Jun 16 '17 at 18:03
4

You can use java.util.BitSet which is a class that represents a large set of positive integers.

class Number {
    public static void main(String[] args) {
        int n = 100;
        String set = new java.util.BitSet() {{ set(1, n+1); }}.toString();
        System.out.append(set, 1, set.length()-1);
    }
}
mrhallak
  • 1,138
  • 1
  • 12
  • 27
4

Since all loops require conditional statements we can simplify the issue to "list an arbitrary range of numbers without conditionals." In java there is no boolean -> integer conversion, bools can only be used in conditionals so we can cross that off of the list.

That leaves us with the arithmetic option. We can fake boolean values with 1's and 0's. To use the 1 or 0 instead of true/false, we could make an array of 2 methods, the first would be our recursive code path and the other be our stop. So all we would need is an algorithm that with a count of 0 returns 1, and a count of any non zero value returns 0.

There are a lot of things that separate 0 from all other numbers, but it is hard to take advantage of without dividing by zero. The property I took advantage of is that 0 sits between the positive and negative numbers. If we abs the set of whole numbers, zero is the only number that has the same number on both sides (1 and 1). Knowing that, we know that abs(n-1/n+1) will be 1 for n=0 and <1 for all other positive number. If we cast the result to an int, it will drop any decimal and leave us with g(0)=1 and g(any positive number)=0. Notice that the n+1 is the denominator for if we had n-1 on the bottom a n of 1 would divide by zero.

To avoid any external code we can replace abs(n) with n*n since n can only be between -1 and 1. And that is it, ((a-1)/(a+1)) * ((a-1)/(a+1)) sure looks wacky but it perfectly does what we want.

interface doMethod {
    void doIt(int start, int stop);
}
private static doMethod[] doList = new doMethod[] {
        new doMethod() { public void doIt(int start, int stop) { printNumbers(start, stop); } },
        new doMethod() { public void doIt(int start, int stop) {}}
};
public static void printNumbers(int start, int stop){
    System.out.println(start);
    //a is our 'count' variable
    int a = stop - start;
    //b is our 'index' variable
    int b = ((a-1)/(a+1)) * ((a-1)/(a+1));
    // doList[0 = recurse, 1 = stopRecursing]
    doList[b].doIt(start + 1, stop);
}
public static void main(String[] args) {
    printNumbers(10, 60);
}
Kyle Berezin
  • 597
  • 4
  • 20
3

Your program will crash because at 61int divisonByZero = 1 / (61 - n);will become int divisonByZero = 1 / 0; Which is a division by zero and raises an exception.

You can use a try-catch to catch the exception but I don't know if you see this as a condition. It's also bad practice to use an exception for this. But below you find how you would implement such a version.

public class Main {

   public static void printNumbers(int n, int stop) {
       System.out.println(n);
       try {
          int divisonByZero = 1 / (stop - n);
          printNumbers(n + 1, stop);
       } catch (ArithmeticException e) {
          System.out.println("program is terminated");
       }
   }

   public static void main(String[] args) {
       printNumbers(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
   }
}
Isabek Tashiev
  • 1,036
  • 1
  • 8
  • 14
Arne Goeteyn
  • 166
  • 9
  • Thanks good suggestion. but as you said it's bad practice to use an exception for this. also even with try catch it's still not a preferable solution because it's handling an exception (runtime error) which is division by zero. – Oghli Jun 06 '17 at 21:14
3

Why not to use simple stream?

IntStream.range(10, 60).forEach(System.out::println)

Stream is not a loop, but you can say "kind of". In your case with division by zero - you can wrap the division statement in try block. And exit without an error.

try { 
    int divisonByZero = 1 / (61 - n);
    ...
catch (ArithmeticException e) {
    // exit or return something to stop recursion
}
Sergei Rybalkin
  • 3,337
  • 1
  • 14
  • 27
  • Thanks. maybe `IntStream.range(10, 60)` work for this problem but `.forEach()` is considered kind of loop and isn't allowed in solving the problem. also as I said in the question even with try catch the Arithmetic Exception it's still not a preferable solution because it's handling an exception (runtime error). – Oghli Jun 13 '17 at 21:45
  • Colt, is this about clean code? If so, this is the only acceptable solution I have seen here. It is clean, elegant, fluent, conveys the programmer's intent, is free of explicit loops, if-conditions, exception handling. With all due respect, your comment about wishing to get away even from the implicit forEach loop is just nonsense to me. – kriegaex Jun 15 '17 at 20:39
  • @kriegaex `.forEach()` is a new method in java 8 and I am not allowed to use it in the solution because it's considered a for loop which implemented in different way. – Oghli Jun 16 '17 at 02:54
  • 1
    I do not care if you are allowed to use it or not in your esoteric task. I do care about clean code, though. Some of the contrived solutions I have seen here to solve your challenge make my toenails curl up. Possible or not, that is not the way to go. If you like to have a nice program flow without loops and if-conditions, just do a little OOD and hide those details in a method, then call that method. The machinery in an engine eventually has to be somehwere, though. – kriegaex Jun 16 '17 at 04:36
3

Am I allowed to use implicit numeric conversions?

public class NoLoopNoConditional {
    @SuppressWarnings( "unchecked" )
    private final Consumer< Integer >[] afn =
        ( Consumer< Integer >[] )new Consumer[ 2 ];
    private final double dDelta;
    private final int iLow;

    public NoLoopNoConditional( int iLow, int iHigh ) {
        this.iLow = iLow;
        this.dDelta = ( double )iHigh + 1 - iLow;
        // load the recursive and terminal cases
        afn[ 0 ] = ( i ) -> {};
        afn[ 1 ] = ( i ) -> {
            System.out.println( i );
            recur( i + 1 );
        };
    }

    // returns 1 until i exceeds iHigh, then returns 0
    private int choice( int i ) {
        return ( int )( ( dDelta + dDelta - i + iLow ) / ( dDelta + 1 ) );
    }

    private void recur( int i ) {
        afn[ choice( i ) ].accept( i );
    }

    public static void main( String[] args ) {
        // grab the parameters
        // throws exception if wrong # of args or can't parse. no conditionals
        int iLow = Integer.parseInt( args[ 0 ] ), iHigh = Integer.parseInt( args[ 1 ] );

        // go for it
        new NoLoopNoConditional( iLow, iHigh ).recur( iLow );
    }
}

The only defect is that a large range will cause a StackOverflowError because of too many (tail-)recursive calls.

Judge Mental
  • 5,209
  • 17
  • 22
  • I noticed that if you reverse high and low, say try to get this to wrap around, like with arguments of `2147483647` for low, and `-2147483648` for high it will throw a `java.lang.IndexOutOfBoundsException` and not just wrap like it should. My implementation handles this. I did still up vote this cause it is cool! – Imposter Jun 16 '17 at 01:38
  • ArrayList.add(int var1, E var2) uses rangeCheckForAdd which uses an if. Although this is a very cool answer. – Kyle Berezin Jun 16 '17 at 03:03
  • Now that's just silly. My first version had a regular array, but I didn't like all the casting and raw types. – Judge Mental Jun 16 '17 at 03:06
  • Oh my bad, I understand it now. Yea that works fine with an array. Take a look at how I did it, I did very similar approach. (though perhaps a bit uglier?) – Kyle Berezin Jun 16 '17 at 03:29
  • Making it an array just to be complete about it, sigh. – Judge Mental Jun 16 '17 at 03:41
  • x) I agree yours is the first legit solution, though he already gave the bounty to the guy that leveraged the conditionals in class.getMethod – Kyle Berezin Jun 16 '17 at 03:46
2

At first, why would you need to resolve any such problem? What prohibits you from the standard cycles and even the standard "IF"? ...This sounds to me like just a scholar hypothetical discuss. :-/

Anyway:

As mentioned already, every repeatable sequence needs an "IF", the condition to stop: It definitely will appear on the processor (imagine the ASM instruction), during the runtime. The only difference is, on which level of abstraction/architecture the IF is caused:

  • Either directly within the code (as the basic level, however prohibited here, in the question) ..no matter, whether syntactically real IF, or by the ternary ?:,
  • ...
  • or i.e. in the JVM inheritance mechanism, on the other edge-extreme of the spectrum of possibilities: The polymorphism does the IF, but internally, implicitly. (Did anyone mention that here yet?) I imagine two mutated-object classes, implementing the same method:

    • In one implementation, the altered method, there would be a hard-stop,
    • in the other just a recursion-call, of the "runtime class".

    Such methods would be pretty short and straightforward.

    ...these could be implementations of an abstract class: It's up to you.

Still, whichever solution-implementation would not change the fact: The IF is still there, somewhere.

Franta
  • 986
  • 10
  • 17
  • Any references? – Imposter Jun 16 '17 at 01:11
  • Are you sure? I'm pretty sure my example doesn't do any of that. I could write it in c if I had to. – Kyle Berezin Jun 16 '17 at 02:58
  • @KyleBerezin: I see this in your code: "doList[b].doIt(start + 1, stop);" - So, you say "there is no IF"? Seriously? The fact, the IF is wrapped/hid does not mean, it could diasapper. Sure, there is an IF, somewhere inside: i.e. the mechanism of overloading uses an IF internally, definitely. (i.e. the interpretter) – Franta Jan 13 '18 at 06:25
1

Using an Exception for normal program flow is poor form, but this works. Eventually there will be a division by zero exception which is the signal to quit.

public class App {

    public static void main(String[] args) {
        App app = new App();
        try {
            app.print(10, 60);
        } catch (ArithmeticException ae) {
            // ignore
        }
    }

    private void print(int next, int until) {
        System.out.println(next);
        assertNotEndOfRange(next, until);
        print(++next, until);
    }

    private int assertNotEndOfRange(int next, int until) {
        return 0 / (until - next);
    }

}
Andrew S
  • 2,509
  • 1
  • 12
  • 14
1

Your code

public static void printNumbers(int n){
       int divisonByZero = 1 / (61 - n);
                               ^^^(1/61-61) => 1/0 => Dividedbyzero error
       System.out.println(n);
       printNumbers(n+1);
}     
public static void main(String[] args) {
       printNumbers(10);
}

You said that the program crashes when it reaches 61.

It is a runtime error

More specifically ArithmeticException

How?

You have this condition

int divisionByZero = 1 / (61-n);

when n reaches 61, then 1 / (61 - 61)

which is equal to 1 / 0 which is an error.

To stop this, you have to implement try...catch to catch arithmetic exceptions

So, the code will be

public static void printNumbers(int n){
       try{
           int divisonByZero = 1 / (61 - n);
           System.out.println(n);
           printNumbers(n+1);
       }catch(ArithmeticException e){
       }
}     
public static void main(String[] args) {
       printNumbers(10);
}
Community
  • 1
  • 1
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • Thanks for clarification. but it's bad practice to use an exception for this.even with try catch it's still not a preferable solution because it's handling an exception (runtime error) which is division by zero. – Oghli Jun 11 '17 at 19:48
1

I don't think it is even theoretically doable. Either way you have to have a stop-condition. It can be:

  • an if
  • a return with a ternary operator
  • an exception check (internally does instanceof)
  • a lambda (a lot of solutions exist, most of them will probably involve one-or-more implicit loops and one-or-more implicit ifs)

If you go to the deepest level humanly comprehensible (a.k.a. assembly), you will definitely have at least one jmp in the resulting program. This fact is independent of the higher-level language you use.

D. Kovács
  • 1,232
  • 13
  • 25
1

here is my code... got idea from imposter. Thanks @imposter

package com.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class StackOverFlow {
    Map<Integer, String> methodCall = new HashMap<>();
    static int diff;
    static int reminder;
    static int methodNumber;
    public static void print1(Integer start, Integer end) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        diff= (end.intValue()-1)-start.intValue();
        reminder = diff % 2;
        methodNumber = reminder+1;
           System.out.println(start.intValue());
           //System.out.println("methodNumber   " + methodNumber);
           Method call =StackOverFlow.class.getDeclaredMethod("print"+methodNumber,Integer.class,Integer.class);
           call.invoke(StackOverFlow.class, start.intValue()+1,end);

    }
    public static void print0(Integer start, Integer end){

           //System.out.println(n.intValue());
           System.exit(0);

    }
    public static void print2(Integer start, Integer end) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        diff= (end.intValue()-1)-start.intValue();
        reminder = diff % 2;
        methodNumber = reminder+1;

           System.out.println(start.intValue());
           //System.out.println("methodNumber   " + methodNumber);
           Method call =StackOverFlow.class.getDeclaredMethod("print"+methodNumber,Integer.class,Integer.class);
           call.invoke(StackOverFlow.class, start.intValue()+1,end);

    } 

    public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

           print1(Integer.valueOf(10),Integer.valueOf(60));
    }

}
0

You can use Java 8 streams as follows:

import java.util.stream.IntStream;
public class StreamApp {
     public static void main(String[] args) {
         IntStream.range(10, 60).forEach(System.out::println);
     }
}

The result:

10
11
12
.
.
.
58
59
Reza Rahimi
  • 958
  • 11
  • 16
-1

I have three solutions without a loop in or condition in the Sourcecode

The first one uses the JavaScript Engine to evaluate a string command which is stored in a byte[] at compile-time.

import javax.script.ScriptEngineManager;
import java.nio.charset.StandardCharsets;


public class PrintNumbers
{
    public static void main(String... args) throws Exception
    {
        byte[] iCantSeeALoopHere = new byte[]{102, 111, 114, 32, 40, 105, 32, 61, 32, 49, 48, 59, 32, 105, 32, 60, 61, 32, 54, 48, 59, 32, 105, 43, 43, 41, 32, 123, 32, 112, 114, 105, 110, 116, 40, 105, 41, 59, 32, 125};
        new ScriptEngineManager().getEngineByName("JavaScript").eval(new String(iCantSeeALoopHere, StandardCharsets.UTF_8));
    }
}

The second one writes a .class-File to the Homedirectory an executes it.

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


public class PrintNumbers
{
    public static void main(String... args) throws Exception
    {
        byte[] iCantSeeALoopHere = new byte[]{-54, -2, -70, -66, 0, 0, 0, 52, 0, 31, 10, 0, 5, 0, 17, 9, 0, 18, 0, 19, 10, 0, 20, 0, 21, 7, 0, 22, 7, 0, 23, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 4, 109, 97, 105, 110, 1, 0, 22, 40, 91, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 41, 86, 1, 0, 13, 83, 116, 97, 99, 107, 77, 97, 112, 84, 97, 98, 108, 101, 1, 0, 10, 69, 120, 99, 101, 112, 116, 105, 111, 110, 115, 7, 0, 24, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 17, 80, 114, 105, 110, 116, 78, 117, 109, 98, 101, 114, 115, 46, 106, 97, 118, 97, 12, 0, 6, 0, 7, 7, 0, 25, 12, 0, 26, 0, 27, 7, 0, 28, 12, 0, 29, 0, 30, 1, 0, 12, 80, 114, 105, 110, 116, 78, 117, 109, 98, 101, 114, 115, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 1, 0, 19, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 69, 120, 99, 101, 112, 116, 105, 111, 110, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 121, 115, 116, 101, 109, 1, 0, 3, 111, 117, 116, 1, 0, 21, 76, 106, 97, 118, 97, 47, 105, 111, 47, 80, 114, 105, 110, 116, 83, 116, 114, 101, 97, 109, 59, 1, 0, 19, 106, 97, 118, 97, 47, 105, 111, 47, 80, 114, 105, 110, 116, 83, 116, 114, 101, 97, 109, 1, 0, 7, 112, 114, 105, 110, 116, 108, 110, 1, 0, 4, 40, 73, 41, 86, 0, 33, 0, 4, 0, 5, 0, 0, 0, 0, 0, 2, 0, 1, 0, 6, 0, 7, 0, 1, 0, 8, 0, 0, 0, 29, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 1, 0, 9, 0, 0, 0, 6, 0, 1, 0, 0, 0, 1, 0, -119, 0, 10, 0, 11, 0, 2, 0, 8, 0, 0, 0, 74, 0, 2, 0, 2, 0, 0, 0, 23, 16, 10, 60, 27, 16, 60, -93, 0, 16, -78, 0, 2, 27, -74, 0, 3, -124, 1, 1, -89, -1, -16, -79, 0, 0, 0, 2, 0, 9, 0, 0, 0, 18, 0, 4, 0, 0, 0, 5, 0, 9, 0, 7, 0, 16, 0, 5, 0, 22, 0, 9, 0, 12, 0, 0, 0, 9, 0, 2, -4, 0, 3, 1, -6, 0, 18, 0, 13, 0, 0, 0, 4, 0, 1, 0, 14, 0, 1, 0, 15, 0, 0, 0, 2, 0, 16};
        Path javaClassFile = Paths.get(System.getProperty("user.home"), "PrintNumbers.class").toAbsolutePath();
        Files.write(javaClassFile, iCantSeeALoopHere);

        new ProcessBuilder(
                "java",
                "-cp",
                javaClassFile.getParent().toString(),
                javaClassFile.getFileName().toString().replace(".class", "")
        ).inheritIO().start().waitFor();

        Files.delete(javaClassFile);
    }
}

The third one uses the getOrDefault-Method of a Map have something like a condition:

import java.util.Map;
import java.util.HashMap;
import java.util.function.BiConsumer;

public class PrintNumbers
{
    private static Map<Integer, BiConsumer<Integer, Integer>> funcMap;

    public static void main(String[] args)
    {
      funcMap = new HashMap<>();
      funcMap.put(0, PrintNumbers::doNothing);

      printNumbers(10, 60);
    }

    private static void printNumbers(int curr, int end)
    {
      System.out.println(curr);

      BiConsumer<Integer, Integer> next = funcMap.getOrDefault(end - curr, PrintNumbers::printNumbers);
      next.accept(curr + 1, end);
    }

    private static void doNothing(int a, int b) {}
}
Felix
  • 2,256
  • 2
  • 15
  • 35