Skip to main content
gamedev.net
🔒 Locked

Java Challange

Started by bratiefanut 4 replies 4.2k views
Original Post
bratiefanut
bratiefanut

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  
fastcall22
fastcall22
public class Wat {
	public static void main(String[] args) {
		CubicRootChallenge.checkCubicRoot(new BigInteger("1"));
		System.out.println("YOU WIN!");
	}
}
deftware
deftware

    fyl2x    
    fld st(0)
    frndint  
    fld1     
    fscale   
    fxch st(2)
    fsubrp   
    f2xm1    
    fld1     
    faddp    
    fmulp    
    ret
[ deftware.org ]
Datamancer
Datamancer

    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 ob
ultramailman
ultramailman

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?

Avalander
Avalander

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.