038 Pandigital multiples.sf 796 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?
  6. # https://projecteuler.net/problem=38
  7. # Runtime: 1.044s (previously: 1.644s)
  8. func valid_num(n) {
  9. var digits = n.digits
  10. return false if (digits.contains(0))
  11. return false if (digits.unique.len != n.len)
  12. return true
  13. }
  14. var max = 0
  15. for i in (1..10000) {
  16. valid_num(i) || next
  17. var prod = Str(i)
  18. for n in (2..9) {
  19. var last = prod
  20. prod += Str(n*i)
  21. if (!valid_num(Num(prod))) {
  22. last = last.to_i
  23. max = last if (max < last)
  24. break
  25. }
  26. }
  27. }
  28. say max