Numbers with same digit set in base 10 and base 16: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
|||
(16 intermediate revisions by 9 users not shown) | |||
Line 32:
</pre>
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN base digitset n:
PUT {} IN set
WHILE n>0:
PUT n mod base IN digit
IF digit not.in set: INSERT digit IN set
PUT floor(n/base) IN n
RETURN set
PUT 0 IN col
FOR n IN {0..100000}:
IF 10 digitset n = 16 digitset n:
WRITE n>>8
PUT col+1 IN col
IF col mod 10 = 0: WRITE /</syntaxhighlight>
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
53 371 913 1040 2080 2339 4100 5141 5412 5441
6182 8200 9241 13593 13665 13969 16406 20530 26946 30979
32803 33638 33840 33841 33842 33843 33844 33845 33846 33847
33848 33849 34883 37943 38931 38966 38995 66310 71444 71497
71511 75120 75121 75122 75123 75124 75125 75126 75127 75128
75129 75621 86150 88165 91465 91769 96617 98711 99481</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_Io;
Line 400 ⟶ 423:
71511 75120 75121 75122 75123 75124 75125 75126 75127 75128
75129 75621 86150 88165 91465 91769 96617 98711 99481</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
function HasSameDigits1016(N: integer): boolean;
{Return true if base-10 string and base-16 string have same characters}
var S10,S16: string;
var I: integer;
begin
Result:=False;
{Get base-10 and -16 string}
S10:=IntToStr(N);
S16:=Format('%x',[N]);
{Compare S10 to S16}
for I:=1 to Length(S10) do
if Pos(S10[I],S16)=0 then exit;
{Compare S16 to S10}
for I:=1 to Length(S16) do
if Pos(S16[I],S10)=0 then exit;
Result:=True;
end;
procedure ShowSameDigits1016(Memo: TMemo);
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
for I:=0 to 100000-1 do
if HasSameDigits1016(I) then
begin
Inc(Cnt);
S:=S+Format('%8D',[I]);
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count='+IntToStr(Cnt));
end;
</syntaxhighlight>
{{out}}
<pre>
0 1 2 3 4
5 6 7 8 9
53 371 913 1040 2080
2339 4100 5141 5412 5441
6182 8200 9241 13593 13665
13969 16406 20530 26946 30979
32803 33638 33840 33841 33842
33843 33844 33845 33846 33847
33848 33849 34883 37943 38931
38966 38995 66310 71444 71497
71511 75120 75121 75122 75123
75124 75125 75126 75127 75128
75129 75621 86150 88165 91465
91769 96617 98711 99481
Count=69
Elapsed Time: 41.373 ms.
</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang=text>
func digset n b .
while n > 0
h = bitshift 1 (n mod b)
set = bitor set h
n = n div b
.
return set
.
for i = 0 to 100000 - 1
if digset i 10 = digset i 16
write i & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 53 371 913 1040 2080 2339 4100 5141 5412 5441 6182 8200 9241 13593 13665 13969 16406 20530 26946 30979 32803 33638 33840 33841 33842 33843 33844 33845 33846 33847 33848 33849 34883 37943 38931 38966 38995 66310 71444 71497 71511 75120 75121 75122 75123 75124 75125 75126 75127 75128 75129 75621 86150 88165 91465 91769 96617 98711 99481
</pre>
=={{header|F_Sharp|F#}}==
Line 804 ⟶ 913:
(98711, 18197)
(99481, 18499)</pre>
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''.
'''Also works with fq, a Go implementation of a large subset of jq'''
<syntaxhighlight lang=jq>
# The def of _nwise/1 is included here in case gojq or fq is used.
def _nwise($n):
def nw: if length <= $n then . else .[0:$n] , (.[$n:] | nw) end;
nw;
def chars: explode[] | [.] | implode;
# decimal number to hex string using lower-case letters
def hex:
def stream:
recurse(if . > 0 then ./16|floor else empty end) | . % 16 ;
if . == 0 then "0"
else [stream] | reverse | .[1:]
| map(if . < 10 then 48 + . else . + 87 end) | implode
end;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
# Emit a stream of same-digit numbers, up to .
def sameDigitSettasktask:
def u: [tostring|chars] | unique;
range(0; .) | select((hex|u) == u);
# The task
1e5
| "Numbers under \(.) which use the same digits in decimal as in hex:",
( [sameDigitSettasktask]
| map(lpad(6))
| ((_nwise(10) | join(" ")),
"\n\(length) such numbers found." ) )
</syntaxhighlight>
{{output}}
<pre>
Numbers under 100000 which use the same digits in decimal as in hex:
0 1 2 3 4 5 6 7 8 9
53 371 913 1040 2080 2339 4100 5141 5412 5441
6182 8200 9241 13593 13665 13969 16406 20530 26946 30979
32803 33638 33840 33841 33842 33843 33844 33845 33846 33847
33848 33849 34883 37943 38931 38966 38995 66310 71444 71497
71511 75120 75121 75122 75123 75124 75125 75126 75127 75128
75129 75621 86150 88165 91465 91769 96617 98711 99481
69 such numbers found.
</pre>
=={{header|Julia}}==
Line 818 ⟶ 978:
71511 75120 75121 75122 75123 75124 75125 75126 75127 75128
75129 75621 86150 88165 91465 91769 96617 98711 99481
</pre>
=={{header|langur}}==
{{trans|Python}}
<syntaxhighlight lang="langur">
# using hash keys to create a pseudo-set
val makeset = fn n:foldfrom(fn h, v: h ~ {v: 0}, {:}, split(n))
var col = 1
for n in 0..99999 {
if makeset(n) == makeset(string(n, 16)) {
write "{{n:7}}"
if col div 10: writeln()
col += 1
}
}
writeln()
</syntaxhighlight>
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
53 371 913 1040 2080 2339 4100 5141 5412 5441
6182 8200 9241 13593 13665 13969 16406 20530 26946 30979
32803 33638 33840 33841 33842 33843 33844 33845 33846 33847
33848 33849 34883 37943 38931 38966 38995 66310 71444 71497
71511 75120 75121 75122 75123 75124 75125 75126 75127 75128
75129 75621 86150 88165 91465 91769 96617 98711 99481
</pre>
Line 1,454 ⟶ 1,641:
done...
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
« → b
« #0
'''WHILE''' OVER '''REPEAT'''
SWAP b IDIV2
ROT SWAP ALOG R→B OR
'''END''' NIP
» » '<span style="color:blue">DIGSET</span>' STO
« { }
0 99999 '''FOR''' n
'''IF''' n 10 <span style="color:blue">DIGSET</span> n 16 <span style="color:blue">DIGSET</span> == '''THEN''' n + '''END'''
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 0 1 2 3 4 5 6 7 8 9 53 371 913 1040 2080 2339 4100 5141 5412 5441 6182 8200 9241 13593 13665 13969 16406 20530 26946 30979 32803 33638 33840 33841 33842 33843 33844 33845 33846 33847 33848 33849 34883 37943 38931 38966 38995 66310 71444 71497 71511 75120 75121 75122 75123 75124 75125 75126 75127 75128 75129 75621 86150 88165 91465 91769 96617 98711 99481 }
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">p (1..100_000).select{|n| n.digits.to_set == n.digits(16).to_set}</syntaxhighlight>
{{out}}
<pre>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 53, 371, 913, 1040, 2080, 2339, 4100, 5141, 5412, 5441, 6182, 8200, 9241, 13593, 13665, 13969, 16406, 20530, 26946, 30979, 32803, 33638, 33840, 33841, 33842, 33843, 33844, 33845, 33846, 33847, 33848, 33849, 34883, 37943, 38931, 38966, 38995, 66310, 71444, 71497, 71511, 75120, 75121, 75122, 75123, 75124, 75125, 75126, 75127, 75128, 75129, 75621, 86150, 88165, 91465, 91769, 96617, 98711, 99481]
</pre>
=={{header|SETL}}==
<syntaxhighlight lang="setl">program Numbers_with_same_digit_set_in_base_10_and_base_16;
loop for i in [0..100000] | digitset(i,10) = digitset(i,16) do
nprint(lpad(str i, 8));
if (col +:= 1) mod 10 = 0 then print; end if;
end loop;
print;
proc digitset(n, b);
s := {};
loop until n=0 do
s with:= n mod b;
n div:= b;
end loop;
return s;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
53 371 913 1040 2080 2339 4100 5141 5412 5441
6182 8200 9241 13593 13665 13969 16406 20530 26946 30979
32803 33638 33840 33841 33842 33843 33844 33845 33846 33847
33848 33849 34883 37943 38931 38966 38995 66310 71444 71497
71511 75120 75121 75122 75123 75124 75125 75126 75127 75128
75129 75621 86150 88165 91465 91769 96617 98711 99481</pre>
=={{header|Sidef}}==
Line 1,492 ⟶ 1,732:
{{libheader|Wren-fmt}}
{{libheader|Wren-set}}
<syntaxhighlight lang="
import "./set" for Set
var limit = 1e5
Line 1,560 ⟶ 1,800:
69 such numbers found.
</pre>
=={{header|Zig}}==
{{trans|C}}
<syntaxhighlight lang="zig">const print = @import("std").debug.print;
fn digitset(numinp: u32, base: u32) u32 {
var set: u32 = 0;
var num: u32 = numinp;
while (num != 0) : (num /= base) {
set |= @as(u32, 1) << @truncate(u5, num % base);
}
return set;
}
pub fn main() void {
var i: u32 = 0;
const LIMIT: u32 = 100000;
while (i < LIMIT) : (i += 1) {
if (digitset(i, 10) == digitset(i, 16)) {
print("{}\n", .{i});
}
}
}</syntaxhighlight>
|