Printing numbers in a certain range without using any cycles or conditions (Java)

Maybe the first idea came to mind to solve such problems is a recursive function, but it will also be difficult to write a recursive function without any conditions.

I tried this approach for printing 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 work when it reaches the number 61without exception handling

also, even when trying to catch an arithmetic exception, this is not the preferred solution, because it handles the exception ( runtime error ).

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

, ++, , , ,

.

+6
16

@Imposter, ,

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 "";
    }
}
+5

- : ( )

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



}
+6

, hashmap, , :

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

, , OO, .

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

:

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 .

+4

java.util.BitSet, , .

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

, 61 int divisonByZero = 1 / (61 - n); int divisonByZero = 1 / 0; Which is a division by zero and raises an exception.

try-catch, , , . . , ​​.

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]));
   }
}
+3

?

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

, "". - try. .

try { 
    int divisonByZero = 1 / (61 - n);
    ...
catch (ArithmeticException e) {
    // exit or return something to stop recursion
}
+3

?

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

, StackOverflowError - () .

+3

, " ". java boolean → integer, bools , .

. 1 0. 1 0 true/false, 2 , , - . , , , , 0 1, 0.

, 0 , , . , , , 0 . , , (1 1). , , abs (n-1/n + 1) 1 n = 0 < 1 . int, g (0) = 1 g ( ) = 0. , n + 1 , n-1 , n 1 .

, abs (n) n * n, n -1 1. ((a-1)/(a ​​+ 1)) * ( (a-1)/(a ​​+ 1)), , , , .

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

-, ​​? "IF"?... , .: -/

:

, "IF", ​​ : ( ASM) . , / :

  • ( , , ). , IF ?:,
  • ...
  • .. JVM , - : IF, , , (- ?) , :

    • , , ,
    • " ".

    .

    ... : .

, , - : IF -.

+2

, . , .

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

}
+1

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

, , 61.

ArithmeticException

?

int divisionByZero = 1 / (61-n);

n 61, 1 / (61 - 61)

1 / 0, .

, try...catch

,

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

, . . :

  • a if
  • a return
  • ( instanceof)
  • ( , , , if s)

- ( a.k.a.), jmp . , .

+1

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

}
+1

Java 8 :

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

:

10
11
12
.
.
.
58
59
0

I have three solutions without a loop or condition in the source code

The first uses the JavaScript Engine to evaluate the string command that is stored in the [] 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 writes the .class file to Homedirectory and 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 an getOrDefault-Mod card has 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) {}
}
-1
source

All Articles