Permuted multiples
Permuted multiples is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Find the smallest positive integer n such that, when expressed in decimal, 2*n, 3*n, 4*n, 5*n, and 6*n contain exactly the same digits.
Ring
<lang ring> load "stdlib.ring"
see "working..." + nl see "Permuted multiples are:" + nl perm2 = [] perm3 = [] perm4 = [] perm5 = [] perm6 = []
for n = 1 to 1000000
perm2 = [] perm3 = [] perm4 = [] perm5 = [] perm6 = [] per2 = 2*n perStr2 = string(per2) for m = 1 to len(perStr2) add(perm2,perStr2[m]) next per3 = 3*n perStr3 = string(per3) for m = 1 to len(perStr3) add(perm3,perStr3[m]) next per4 = 4*n perStr4 = string(per4) for m = 1 to len(perStr4) add(perm4,perStr4[m]) next per5 = 5*n perStr5 = string(per5) for m = 1 to len(perStr5) add(perm5,perStr5[m]) next per6 = 6*n perStr6 = string(per6) for m = 1 to len(perStr6) add(perm6,perStr6[m]) next perm2 = sort(perm2) perm3 = sort(perm3) perm4 = sort(perm4) perm5 = sort(perm5) perm6 = sort(perm6) perStr2 = list2str(perm2) perStr3 = list2str(perm3) perStr4 = list2str(perm4) perStr5 = list2str(perm5) perStr6 = list2str(perm6) perStr2 = substr(perStr2,nl,"") perStr3 = substr(perStr3,nl,"") perStr4 = substr(perStr4,nl,"") perStr5 = substr(perStr5,nl,"") perStr6 = substr(perStr6,nl,"") if perStr2 = perStr3 and perStr2 = perStr4 and perStr2 = perStr5 and perStr2 = perStr6 see "n = " + n + nl see "2*n = " + (n*2) + nl see "3*n = " + (n*3) + nl see "4*n = " + (n*4) + nl see "5*n = " + (n*5) + nl see "6*n = " + (n*6) + nl exit ok
next
see "done..." + nl </lang>
- Output:
working... Permuted multiples are: n = 142857 2*n = 285714 3*n = 428571 4*n = 571428 5*n = 714285 6*n = 857142 done...
Wren
One thing that's immediately clear is that the number must begin with '1' otherwise the higher multiples will have more digits than it has. <lang ecmascript>import "/math" for Int
// assumes l1 is sorted but l2 is not var areSame = Fn.new { |l1, l2|
if (l1.count != l2.count) return false l2.sort() for (i in 0...l1.count) { if (l1[i] != l2[i]) return false } return true
}
var i = 100 // clearly a 1 or 2 digit number is impossible var nextPow = 1000 while (true) {
var digits = Int.digits(i) if (digits[0] != 1) { i = nextPow nextPow = nextPow * 10 continue } digits.sort() var allSame = true for (j in 2..6) { var digits2 = Int.digits(i * j) if (!areSame.call(digits, digits2)) { allSame = false break } } if (allSame) { System.print("The smallest positive integer n for which the following") System.print("multiples contain exactly the same digits is:") System.print(" n = %(i)") for (k in 2..6) System.print("%(k) x n = %(k * i)") return } i = i + 1
}</lang>
- Output:
The smallest positive integer n for which the following multiples contain exactly the same digits is: n = 142857 2 x n = 285714 3 x n = 428571 4 x n = 571428 5 x n = 714285 6 x n = 857142