Advertisement

Java Challange

Started by April 30, 2015 05:52 PM
4 comments, last by Avalander 9 years, 8 months ago

Ok guys,

Here is a challange for you :D.
The GOAL is to make a single separate .java file that calls CubicRootChallenge.checkCubicRoot() and is able to get true.

You are not able to use reflection.


import java.math.BigInteger;
import java.security.SecureRandom;
 
/**
 * RULES:
 *
 * <p>1. Write a class that by calling CubicRootChallenge.checkCubicRoot(), is able to reach the *GOAL* line
 *
 * <p>2. Solve the problem in a single separate .java file which compiles and runs with JDK 6/7/8
 *
 * <p>3. Your code must run with the security manager enabled (java -Djava.security.manager your.Class)
 *
 *
 */
public class CubicRootChallenge {
 
        // Number of BITS for the number
        public static final int BITS = 10000;
 
        // Generate random number with said bits [0 <--> 2^BITS - 1], and elevates it ^3
        private static final BigInteger NUMBER = new BigInteger(BITS, new SecureRandom()).pow(3);
 
        // Methods that prints out when passed BigInteger is cubic root of NUMBER
        public static void checkCubicRoot(BigInteger numberToTest) {
 
                // Check if numberToTest is cubic root of NUMBER
                if (NUMBER.divide(numberToTest).divide(numberToTest).equals(numberToTest)) {
 
                        // THE GOAL IS TO REACH THIS LINE!
                        System.out.println("YOU WIN!");
                }
        }
}

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

public class Wat {
	public static void main(String[] args) {
		CubicRootChallenge.checkCubicRoot(new BigInteger("1"));
		System.out.println("YOU WIN!");
	}
}
Advertisement

    fyl2x    
    fld st(0)
    frndint  
    fld1     
    fscale   
    fxch st(2)
    fsubrp   
    f2xm1    
    fld1     
    faddp    
    fmulp    
    ret

[ deftware.org ]


    public class CubicRootChallenge {
        public static boolean checkCubicRoot() {
            return true;
        }
    } 

public class Wat {
    public static void main( String[] arg ) {
        //I'll take my prize now ;)
        CubicRootChallenge.checkCubicRoot();
    }
}

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

Ooo can somebody explain deftware's code? I didn't know .java files can look like that (looks like some kind of assembly?). How does it call checkCubicRoot? Also Mr L, are you accepting trick answers or are you really looking for a specific mathy solution?


public static void main(String[] args) {
    while (true) {
        CubicRootChallenge.checkCubicRoot(new BigInteger(CubicRootChallenge.BITS, new SecureRandom()));
    }
}

This topic is closed to new replies.

Advertisement