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.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);intqueries=scanner.nextInt();for(inti=0;i<queries;i++){intn=scanner.nextInt();System.out.println(findTriangleNumberWithDivisors(n));}scanner.close();}privatestaticlongfindTriangleNumberWithDivisors(intn){intmaxDivisors=0;longresult=0;intcurrentNumber=1;while(true){longtriangularNumber=calculateTriangularNumber(currentNumber);intdivisors=countDivisors(triangularNumber);if(divisors>maxDivisors){maxDivisors=divisors;result=triangularNumber;}if(maxDivisors>n){break;}currentNumber++;}returnresult;}privatestaticlongcalculateTriangularNumber(intn){returnn*(n+1)/2;}privatestaticintcountDivisors(longnumber){intcount=0;for(inti=1;i<=Math.sqrt(Double.valueOf(number));i++){// Use Math.sqrt(double) instead of Math.sqrt(long)if(number%i==0){if(number/i==i){count++;}else{count+=2;}}}returncount;}}
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #12: Highly divisible triangular number
You are viewing a single comment's thread. Return to all comments →
Test case 6 & 7 fails
`