045 Triangular pentagonal and hexagonal.sf 441 B

123456789101112131415161718192021
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # It can be verified that T285 = P165 = H143 = 40755.
  6. # Find the next triangle number that is also pentagonal and hexagonal.
  7. # https://projecteuler.net/problem=45
  8. # Runtime: 0.793s
  9. for n in (144 .. Inf) {
  10. var h = n*(2*n - 1);
  11. if ((sqrt(1 + 24*h).inc %% 6) && (sqrt(1 + 8*h).dec %% 2)) {
  12. say h;
  13. break;
  14. }
  15. }