Permuted multiples: Difference between revisions

Add Cowgol
(Add CLU)
(Add Cowgol)
Line 181:
end
end start_up</lang>
{{out}}
<pre>1 * n = 142857
2 * n = 285714
3 * n = 428571
4 * n = 571428
5 * n = 714285
6 * n = 857142</pre>
 
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
 
# Return the amount of times each digit appears in a number
# (as long as none appears more than 9 times that is)
sub digit_set(n: uint32): (set: uint32) is
var ten_powers: uint32[] := {
1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000
};
set := 0;
while n>0 loop
var digit := (n % 10) as uint8;
n := n / 10;
set := set + ten_powers[digit];
end loop;
end sub;
 
# See if for an integer N, [1..6]*N all have the same digits
sub permuted_multiple(n: uint32): (ok: uint8) is
ok := 0;
var ds := digit_set(n);
var i: uint32 := 2;
while i<=6 loop
if ds != digit_set(i * n) then return; end if;
i := i + 1;
end loop;
ok := 1;
end sub;
 
# Find the first matching number
var n: uint32 := 123;
while permuted_multiple(n) == 0 loop
n := n + 1;
end loop;
 
# Print the number and its multiples
var i: uint32 := 1;
while i<=6 loop
print_i32(i);
print(" * n = ");
print_i32(n * i);
print_nl();
i := i+1;
end loop;</lang>
{{out}}
<pre>1 * n = 142857
2,114

edits