A problem which appeared in Brilliant.org
/*Find the sum of all five-digit positive integers that
1.are square numbers
2.have all digits square
3.and are NOT ordered numbers.
Clarifications:
•An ordered number is one whose digits are in an increasing sequence. For example, is an ordered number but is not; is an ordered number but is not.
•A square is one that can be obtained by multiplying a number by itself (in particular, is a square).
*/
import java.util.ArrayList;
import java.util.Collection;
public class FreakyNumbers {
public static int num1=00000;
public static int num2=99999;
public static int our_num;
public static int sum;
public static Collection <Integer>num_storage = new ArrayList<>();
public static Collection <Integer>orderednum_storage = new ArrayList<>();
public static Collection <Integer>alldigitssquare = new ArrayList<>();
public static void unordered()
{
String num;
int temp;
for (int i=1;i<=9;i++)
{
for (int j=i;j<=9;j++)
{
for (int k=j;k<=9;k++)
{
for (int l=k;l<=9;l++)
{
for (int m=l;m<=9;m++)
{
num= Integer.toString(i) + Integer.toString(j) +
Integer.toString(k) + Integer.toString(l) + Integer.toString(m);
temp=Integer.parseInt(num);
orderednum_storage.add(temp);
}
}
}
}
}
for (int i=0;i<=9;i++)
{
for (int j=0;j<=9;j++)
{
for (int k=0;k<=9;k++)
{
for (int l=0;l<=9;l++)
{
for (int m=0;m<=9;m++)
{
if(((i!=0)&&(i!=1)&&(i!=4)&&(i!=9))||
((j!=0)&&(j!=1)&&(j!=4)&&(j!=9))||
((k!=0)&&(k!=1)&&(k!=4)&&(k!=9))||
((l!=0)&&(l!=1)&&(l!=4)&&(l!=9))||
((m!=0)&&(m!=1)&&(m!=4)&&(m!=9)))
{
num= Integer.toString(i) +
Integer.toString(j) +
Integer.toString(k) +
Integer.toString(l) +
Integer.toString(m);
temp=Integer.parseInt(num);
if(temp>=10000&&temp<=99999)
{
alldigitssquare.add(temp);
}
}
}
}
}
}
}
}
public static void main (String[] args)
{
FreakyNumbers.unordered();
int temp;
int temp1 = 0;
for (int i=0;i<=num2;i++)
{
temp =i*i;
if ((temp>=10000)&&(temp<=num2))
{
our_num=temp; //getting the squared numbers
num_storage.add(our_num);
// System.out.println("temp is"+our_num);
}
if(temp>num2)
{
break;
}
}
num_storage.removeAll(orderednum_storage);
num_storage.removeAll(alldigitssquare);
for (Integer s : num_storage)
{
temp1=temp1+s;
System.out.println("Summed value"+temp1);
}
}
}