Permuted multiples: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: Add some output formatting) |
(Added Go) |
||
Line 7: | Line 7: | ||
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 but in a different order. |
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 but in a different order. |
||
<br><br> |
<br><br> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|Go-rcu}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"rcu" |
|||
"sort" |
|||
) |
|||
// assumes l1 is sorted but l2 is not |
|||
func areSame(l1, l2 []int) bool { |
|||
if len(l1) != len(l2) { |
|||
return false |
|||
} |
|||
sort.Ints(l2) |
|||
for i := 0; i < len(l1); i++ { |
|||
if l1[i] != l2[i] { |
|||
return false |
|||
} |
|||
} |
|||
return true |
|||
} |
|||
func main() { |
|||
i := 100 // clearly a 1 or 2 digit number is impossible |
|||
nextPow := 1000 |
|||
for { |
|||
digits := rcu.Digits(i, 10) |
|||
if digits[0] != 1 { |
|||
i = nextPow |
|||
nextPow *= 10 |
|||
continue |
|||
} |
|||
sort.Ints(digits) |
|||
allSame := true |
|||
for j := 2; j <= 6; j++ { |
|||
digits2 := rcu.Digits(i*j, 10) |
|||
if !areSame(digits, digits2) { |
|||
allSame = false |
|||
break |
|||
} |
|||
} |
|||
if allSame { |
|||
fmt.Println("The smallest positive integer n for which the following") |
|||
fmt.Println("multiples contain exactly the same digits is:") |
|||
fmt.Println(" n =", i) |
|||
for k := 2; k <= 6; k++ { |
|||
fmt.Printf("%d x n = %d\n", k, k*i) |
|||
} |
|||
return |
|||
} |
|||
i = i + 1 |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang perl6>put display (^∞).map(1 ~ *).race.map( -> \n { next unless [eq] (2,3,4,5,6).map: { (n × $_).comb.sort.join }; n } ).first; |
<lang perl6>put display (^∞).map(1 ~ *).race.map( -> \n { next unless [eq] (2,3,4,5,6).map: { (n × $_).comb.sort.join }; n } ).first; |