1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #!/usr/bin/ruby
- # Author: Daniel "Trizen" Șuteu
- # License: GPLv3
- # Website: https://github.com/trizen
- # 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?
- # https://projecteuler.net/problem=38
- # Runtime: 1.044s (previously: 1.644s)
- func valid_num(n) {
- var digits = n.digits
- return false if (digits.contains(0))
- return false if (digits.unique.len != n.len)
- return true
- }
- var max = 0
- for i in (1..10000) {
- valid_num(i) || next
- var prod = Str(i)
- for n in (2..9) {
- var last = prod
- prod += Str(n*i)
- if (!valid_num(Num(prod))) {
- last = last.to_i
- max = last if (max < last)
- break
- }
- }
- }
- say max
|