Advertisement
UF6

Problem 1

UF6
Feb 28th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. public class P45 {
  2.  
  3.     public static void main(String[] args) {
  4.         long H = 0;
  5.         long i = 144;
  6.         while(true) {
  7.             H = i*((i<<1)-1);
  8.             if ( isPentagonal(H) && isTriangle(H) ) {
  9.                 break;
  10.             }
  11.             i++;
  12.         }
  13.         System.out.println(H);
  14.     }
  15.  
  16.     private static boolean isPentagonal(long x) {
  17.         double n = (1 + Math.sqrt(24*x+1)) / 6;
  18.         return n == (long)n;
  19.     }
  20.  
  21.     private static boolean isTriangle(long x) {
  22.         double n = (-1 + Math.sqrt((x<<3)+1)) / 2;
  23.         return n == (long)n;
  24.     }
  25.  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement