We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerin=newScanner(System.in);longn=in.nextLong();inta=in.nextInt();intb=in.nextInt();intincrement=4;for(longi=1;i<n;i++){if(a==3){if(isTriangularNumber(i))System.out.println(i);if(i==1)i+=3;elseif(i>4){i+=increment+2;increment+=3;}}else{if(isHexagonalNumber(i))System.out.println(i);if(i==1)i+=3;elseif(i>4){i+=increment+2;increment+=3;}}}}publicstaticbooleanisTriangularNumber(longn){doubletTest=Math.sqrt(1+8*n);returntTest==((long)tTest);}privatestaticbooleanisHexagonalNumber(longnumber){doublehexTest=(Math.sqrt(1+8*number)+1.0)/4.0;returnhexTest==((long)hexTest);}}
Project Euler #45: Triangular, pentagonal, and hexagonal
You are viewing a single comment's thread. Return to all comments →
JAva code