The title

N.Wiener, an American mathematician, was precocious and went to college at the age of 11. In 1935~1936, he was invited to give a lecture at Tsinghua University in China. He attended an important conference, and his young face was conspicuous. When asked his age, he replied:

“The cube of my age is a four-digit number. My age to the fourth power is a six-digit number. The 10 digits contain the 10 digits from 0 to 9, each occurring exactly once.

Please calculate how young he was.

Train of thought

Define integer type age, string age3,age4; Use the for loop to check whether the string length of age3 and age4 meets the condition. Add two strings age3 and age4, use the set interface to instantiate a Hashset (Hashset is unordered and does not allow repeating elements), input each character age3 and age4 into the set through the for loop, and output a non-repeating string of length 10

code

import java.util.HashSet;
import java.util.Set;

public class T1 {

public static void main(String[] args) {
       for (int age=11; age<=100; age++) {String age3=age*age*age+"";// Compute the age cube
    	   String age4=age*age*age*age+"";// Compute the age to the fourth power
    	   if(age3.length()==4 && age4.length() == 6 && CheckTenAge(age3,age4))
    	   {
    		   System.out.println(age);
    		   break;
    	   }
       }
	}
public static boolean CheckTenAge(String age3,String age4)
{
	String str = age3+age4;
	Set<Character> set = new HashSet<Character>();
	for (int i=0; i<str.length(); i++){ set.add(str.charAt(i)); }return set.size() == 10; }}Copy the code

The results of

18