Numbers with same digit set in base 10 and base 16
Find decimal numbers n that when converted to hexadecimal produce a number that uses the same set of digits (regardless of order and ignoring duplicates) as the original number, where n < 100000
- Task
- Example
The decimal number 2339 is 923 when written in hexadecimal.
The set of digits used, ignoring order and duplicates, is {2, 3, 9} in both cases and hence this number satisfies the task requirements.
11l
V col = 0
L(i) 100000
I Set(Array(String(i))) == Set(Array(hex(i)))
col++
print(‘#7’.format(i), end' "\n"[0 .< Int(col % 10 == 0)])
print()
- Output:
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
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 /
- Output:
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
Ada
with Ada.Text_Io;
procedure Same_Digits is
Columns : constant := 10;
Width : constant := 8;
type Set_Type is array (0 .. 15) of Boolean;
function Digit_Set_Of (N : Natural; Base : Natural) return Set_Type is
Nn : Natural := N;
Set : Set_Type := (others => False);
begin
while Nn /= 0 loop
Set (Nn mod Base) := True;
Nn := Nn / Base;
end loop;
return Set;
end Digit_Set_Of;
package Natural_Io is new Ada.Text_Io.Integer_Io (Natural);
use Ada.Text_Io, Natural_Io;
Count : Natural := 0;
begin
for N in 0 .. 99_999 loop
if Digit_Set_Of (N, Base => 10) = Digit_Set_Of (N, Base => 16) then
Count := Count + 1;
Put (N, Width => Width);
if Count mod Columns = 0 then
New_Line;
end if;
end if;
end loop;
New_Line;
Put ("Total numbers: "); Put (Count, Width => 3); New_Line;
end Same_Digits;
- Output:
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 Total numbers: 69
ALGOL 68
Generates the numbers in base 16 that only use the digits 0-9.
BEGIN # find numbers that use the same digits in decimal and hexadecimal #
# translation of the Algol W sample #
INT max number = 100 000;
# note 100 000 is 186A0 in hexadecimal, so we need to consider numbers #
# with up to 5 hexadedcimal digits #
# powers of 16 #
INT x2 = 16; INT x3 = x2 * 16; INT x4 = x3 * 16; INT x5 = x4 * 16;
# construct the numbers that have only 0-9 in their hex representations #
INT hd count := 0;
INT n := 0;
INT hx5 := 0;
FOR h5 FROM 0 TO 1 WHILE n < max number DO
INT hx4 := 0;
FOR h4 FROM 0 TO 9 WHILE n < max number DO
INT hx3 := 0;
FOR h3 FROM 0 TO 9 WHILE n < max number DO
INT hx2 := 0;
FOR h2 FROM 0 TO 9 WHILE n < max number DO
FOR h1 FROM 0 TO 9 WHILE ( n := hx5 + hx4 + hx3 + hx2 + h1 ) < max number DO
INT d := n;
BITS dset := IF d = 0 THEN 16r1 ELSE 16r0 FI;
WHILE d > 0 DO
dset := dset OR ( 16r1 SHL ( d MOD 10 ) );
d OVERAB 10
OD;
BITS hset := 16r0;
IF 0 /= h5 THEN hset := hset OR ( 16r1 SHL h5 ) FI;
IF 0 /= h5 + h4 THEN hset := hset OR ( 16r1 SHL h4 ) FI;
IF 0 /= h5 + h4 + h3 THEN hset := hset OR ( 16r1 SHL h3 ) FI;
IF 0 /= h5 + h4 + h3 + h2 THEN hset := hset OR ( 16r1 SHL h2 ) FI;
hset := hset OR ( 16r1 SHL h1 );
IF dset = hset THEN
# the decimal and hexadecimal representations #
# use the same digits #
print( ( " ", whole( n, -7 ) ) );
IF ( hd count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
FI
OD;
hx2 +:= x2
OD;
hx3 +:= x3
OD;
hx4 +:= x4
OD;
hx5 +:= x5
OD;
print( ( newline ) );
print( ( "Found ", whole( hd count, 0 ), " numbers up to ", whole( max number, 0 ) ) );
print( ( " where the decimal and hexadecimal representations use the same digits", newline ) )
END
- Output:
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 Found 69 numbers up to 100000 where the decimal and hexadecimal representations use the same digits
Alternative version testing each number.
BEGIN # find numbers with the same digit set in base 10 and base 16 #
# translation of the EasyLang sample #
PROC digit set = ( INT n in, b )BITS:
BEGIN
INT n := n in;
BITS dset := 16r0;
WHILE n > 0 DO
BITS h = 16r1 SHL ( n MOD b );
dset := dset OR h;
n OVERAB b
OD;
dset
END # digit set # ;
INT max number = 100 000;
INT hd count := 0;
FOR i FROM 0 TO max number - 1 DO
IF digit set( i, 10 ) = digit set( i, 16 ) THEN
print( ( " ", whole( i, -7 ) ) );
IF ( hd count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
FI
OD;
print( ( newline ) );
print( ( "Found ", whole( hd count, 0 ), " numbers up to ", whole( max number, 0 ) ) );
print( ( " where the decimal and hexadecimal representations use the same digits", newline ) )
END
- Output:
Same as the first version.
ALGOL W
Generates the candidate numbers (ones that only have the digits 0-9 in their hexadecimal representation) to cut down the numbers to check.
begin % find numbers that use the same digits in decimal and hexadecimal %
integer hdCount;
integer x2, x3, x4, x5, hx2, hx3, hx4, hx5;
hdCount := 0;
% note 100 000 is 186A0 in hexadecimal, so we need to consider numbers %
% with up to 5 hexadedcimal digits %
% powers of 16 %
x2 := 16; x3 := x2 * 16; x4 := x3 * 16; x5 := x4 * 16;
% construct the numbers that have only 0-9 in their hex representations %
hx5 := 0;
for h5 := 0 until 1 do begin
hx4 := 0;
for h4 := 0 until 9 do begin
hx3 := 0;
for h3 := 0 until 9 do begin
hx2 := 0;
for h2 := 0 until 9 do begin
for h1 := 0 until 9 do begin
integer n, d;
bits hSet, dSet;
n := d := hx5 + hx4 + hx3 + hx2 + h1;
if n > 100000 then goto endSearch;
d := n;
dSet := if d = 0 then #1 else #0;
while d > 0 do begin
dSet := dSet or ( #1 shl ( d rem 10 ) );
d := d div 10
end;
hSet := #0;
if 0 not = h5 then hset := hset or ( #1 shl h5 );
if 0 not = h5 + h4 then hset := hset or ( #1 shl h4 );
if 0 not = h5 + h4 + h3 then hset := hset or ( #1 shl h3 );
if 0 not = h5 + h4 + h3 + h2 then hset := hset or ( #1 shl h2 );
hset := hset or ( #1 shl h1 );
if dset = hset then begin
% the decimal and hexadecimal representations %
% use the same digits %
writeon( i_w := 7, s_w := 0, " ", n );
hdCount := hdCount + 1;
if hdCount rem 10 = 0 then write()
end if_allSame ;
end for_h1 ;
hx2 := hx2 + x2
end for_h2 ;
hx3 := hx3 + x3
end for_h3 ;
hx4 := hx4 + x4
end for_h4 ;
hx5 := hx5 + x5
end for_h5 ;
endSearch:
write( i_w := 1, s_w := 0, "Found ", hdCount, " numbers up to 100000 " );
writeon( "where the decimal and hexadecimal representations use the same digits" )
end.
- Output:
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 Found 69 numbers up to 100000 where the decimal and hexadecimal representations use the same digits
APL
(⊢(/⍨)(≡/((⊂∘⍋⌷⊢)∘∪¨10 16(⊥⍣¯1)¨⊂))¨)0,⍳99999
- Output:
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
AppleScript
use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
use sorter : script "Insertion sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>
on sameDigitSetDecHex(n)
(* Integer number input assumed.
Numbers whose hex LSD > 9 are eliminated immediately. Ditto negatives.
Thereafter the hex form's tested first so that the dec form doesn't have to be if the hex is unsuitable. *)
tell n mod 16 to if ((it > 9) or (it < 0)) then return false
script o
on digitsOf(n, base)
set digits to {n mod base}
set n to n div base
repeat until (n = 0)
set d to n mod base
if (d > 9) then return false
if (d is not in digits) then set end of digits to d
set n to n div base
end repeat
tell sorter to sort(digits, 1, -1)
return digits
end digitsOf
end script
tell o's digitsOf(n, 16) to return ((it is not false) and (it = o's digitsOf(n, 10)))
end sameDigitSetDecHex
local output, n
set output to {}
repeat with n from 0 to 99999
if (sameDigitSetDecHex(n)) then set end of output to n
end repeat
return output
- Output:
{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}
Arturo
valid?: function [n][
equal? sort unique digits n
sort unique digits.base:16 n
]
print select 0..100000 => valid?
- Output:
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
AWK
# syntax: GAWK -f DECIMAL_-_HEXADECIMAL_NUMBERS.AWK
BEGIN {
start = 0
stop = 99999
for (i=start; i<=stop; i++) {
tmp = sprintf("%X",i)
leng_dec = length(i)
leng_hex = length(tmp)
hits_dec = hits_hex = 0
for (j=1; j<=leng_dec; j++) {
if (tmp ~ substr(i,j,1)) {
hits_dec++
}
}
if (leng_dec == hits_dec) {
for (j=1; j<=leng_hex; j++) {
if (i ~ substr(tmp,j,1)) {
hits_hex++
}
}
}
if (leng_hex == hits_hex) {
printf("%6d%1s",i,++count%10?"":"\n")
}
}
printf("\nDecimal matches hexadecimal %d-%d: %d\n",start,stop,count)
exit(0)
}
- Output:
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 Decimal matches hexadecimal 0-99999: 69
C
#include <stdio.h>
#define LIMIT 100000
int digitset(int num, int base) {
int set;
for (set = 0; num; num /= base)
set |= 1 << num % base;
return set;
}
int main() {
int i, c = 0;
for (i = 0; i < LIMIT; i++)
if (digitset(i,10) == digitset(i,16))
printf("%6d%c", i, ++c%10 ? ' ' : '\n');
printf("\n");
return 0;
}
- Output:
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
C++
#include <iostream>
#include <iomanip>
#include <bitset>
const int LIMIT = 100000;
std::bitset<16> digitset(int num, int base) {
std::bitset<16> set;
for (; num; num /= base) set.set(num % base);
return set;
}
int main() {
int c = 0;
for (int i=0; i<LIMIT; i++) {
if (digitset(i,10) == digitset(i,16)) {
std::cout << std::setw(7) << i;
if (++c % 10 == 0) std::cout << std::endl;
}
}
std::cout << std::endl;
return 0;
}
- Output:
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
Cowgol
include "cowgol.coh";
const LIMIT := 100000;
sub digitset(n: uint32, base: uint8): (set: uint16) is
var one: uint16 := 1;
set := 0;
while n != 0 loop
set := set | (one << (n % base as uint32) as uint8);
n := n / base as uint32;
end loop;
end sub;
var i: uint32 := 0;
var c: uint8 := 0;
while i < LIMIT loop
if digitset(i,10) == digitset(i,16) then
print_i32(i);
if c<9 then
c := c + 1;
print_char('\t');
else
c := 0;
print_nl();
end if;
end if;
i := i + 1;
end loop;
print_nl();
- Output:
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
Draco
proc nonrec digitset(ulong n; byte b) word:
word set;
set := 0;
while n ~= 0 do
set := set | 1 << make(n % b, byte);
n := n / b
od;
set
corp
proc nonrec main() void:
ulong MAX = 100000;
ulong n;
byte col;
n := 0;
col := 0;
while n<MAX do
if digitset(n,10) = digitset(n,16) then
write(n:8);
col := col + 1;
if col % 10 = 0 then writeln() fi
fi;
n := n + 1;
od;
writeln()
corp
- Output:
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
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;
- Output:
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.
EasyLang
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 & " "
.
.
- Output:
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
F#
// Decimal-Hexdecimal: Nigel Galloway. June 7th., 2021
let rec fN g=function n when n<10->Some(Set.add n g)|n when n%16<10->fN(g.Add(n%16))(n/16) |_->None
let rec fG n g=match n/10,n%10 with (0,n)->Some(Set.add n g)|(n,i)->fG n (Set.add i g)
let g=Set.empty in seq{0..100000}|>Seq.filter(fun n->fN g n=fG n g)|>Seq.iter(printf "%d "); printfn ""
- Output:
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
Factor
USING: formatting grouping io kernel math.parser present
sequences sets ;
100,000 <iota> [ dup present swap >hex set= ] filter
10 group [ [ "%5d " printf ] each nl ] each
- Output:
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
FreeBASIC
function is_sds( byval n as integer ) as boolean
dim as boolean in_dec(0 to 9), in_hex(0 to 9)
dim as integer nd = n, dh
while nd
in_dec( nd mod 10 ) = true
nd \= 10
wend
while n
dh = n mod 16
if dh > 9 then return false
in_hex( dh ) = true
n \= 16
wend
for i as uinteger = 0 to 9
if in_dec(i) <> in_hex(i) then return false
next i
return true
end function
dim as integer i, c=0
for i = 0 to 99999
if is_sds(i) then
print using "##### ";i;
c+=1
if c mod 10 = 0 then print
end if
next i
- Output:
0 1 2 3 4 5 6 7 8 953 371 913 1040 2080 2339 4100 5141 5412 5441 6182 8200 9241 13593 13665 13969 16406 20530 26946 3097932803 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
Go
package main
import (
"fmt"
"rcu"
"strconv"
)
func equalSets(s1, s2 map[rune]bool) bool {
if len(s1) != len(s2) {
return false
}
for k, _ := range s1 {
_, ok := s2[k]
if !ok {
return false
}
}
return true
}
func main() {
const limit = 100_000
count := 0
fmt.Println("Numbers under 100,000 which use the same digits in decimal or hex:")
for n := 0; n < limit; n++ {
h := strconv.FormatInt(int64(n), 16)
hs := make(map[rune]bool)
for _, c := range h {
hs[c] = true
}
ns := make(map[rune]bool)
for _, c := range strconv.Itoa(n) {
ns[c] = true
}
if equalSets(hs, ns) {
count++
fmt.Printf("%6s ", rcu.Commatize(n))
if count%10 == 0 {
fmt.Println()
}
}
}
fmt.Printf("\n\n%d such numbers found.\n", count)
}
- Output:
Numbers under 100,000 which use the same digits in decimal or hex: 0 1 2 3 4 5 6 7 8 9 53 371 913 1,040 2,080 2,339 4,100 5,141 5,412 5,441 6,182 8,200 9,241 13,593 13,665 13,969 16,406 20,530 26,946 30,979 32,803 33,638 33,840 33,841 33,842 33,843 33,844 33,845 33,846 33,847 33,848 33,849 34,883 37,943 38,931 38,966 38,995 66,310 71,444 71,497 71,511 75,120 75,121 75,122 75,123 75,124 75,125 75,126 75,127 75,128 75,129 75,621 86,150 88,165 91,465 91,769 96,617 98,711 99,481 69 such numbers found.
Haskell
import qualified Data.Set as S
import Numeric (showHex)
---- NUMBERS WITH THE SAME DIGIT SET IN DECIMAL AND HEX --
sameDigitSet :: (Integral a, Show a) => a -> [(a, String)]
sameDigitSet n =
[ (n, h)
| let h = showHex n "",
S.fromList h == S.fromList (show n)
]
--------------------------- TEST -------------------------
main = do
print ("decimal", "hex")
mapM_ print $ [0 .. 100000] >>= sameDigitSet
- Output:
("decimal","hex") (0,"0") (1,"1") (2,"2") (3,"3") (4,"4") (5,"5") (6,"6") (7,"7") (8,"8") (9,"9") (53,"35") (371,"173") (913,"391") (1040,"410") (2080,"820") (2339,"923") (4100,"1004") (5141,"1415") (5412,"1524") (5441,"1541") (6182,"1826") (8200,"2008") (9241,"2419") (13593,"3519") (13665,"3561") (13969,"3691") (16406,"4016") (20530,"5032") (26946,"6942") (30979,"7903") (32803,"8023") (33638,"8366") (33840,"8430") (33841,"8431") (33842,"8432") (33843,"8433") (33844,"8434") (33845,"8435") (33846,"8436") (33847,"8437") (33848,"8438") (33849,"8439") (34883,"8843") (37943,"9437") (38931,"9813") (38966,"9836") (38995,"9853") (66310,"10306") (71444,"11714") (71497,"11749") (71511,"11757") (75120,"12570") (75121,"12571") (75122,"12572") (75123,"12573") (75124,"12574") (75125,"12575") (75126,"12576") (75127,"12577") (75128,"12578") (75129,"12579") (75621,"12765") (86150,"15086") (88165,"15865") (91465,"16549") (91769,"16679") (96617,"17969") (98711,"18197") (99481,"18499")
J
Decimal numbers on left:
require'convert'
(":,.' ',.;@hfd"1),.I.(": -:&~.&(/:~) hfd)"0 i.1e5
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
53 35
371 173
913 391
1040 410
2080 820
2339 923
4100 1004
5141 1415
5412 1524
5441 1541
6182 1826
8200 2008
9241 2419
13593 3519
13665 3561
13969 3691
16406 4016
20530 5032
26946 6942
30979 7903
32803 8023
33638 8366
33840 8430
33841 8431
33842 8432
33843 8433
33844 8434
33845 8435
33846 8436
33847 8437
33848 8438
33849 8439
34883 8843
37943 9437
38931 9813
38966 9836
38995 9853
66310 10306
71444 11714
71497 11749
71511 11757
75120 12570
75121 12571
75122 12572
75123 12573
75124 12574
75125 12575
75126 12576
75127 12577
75128 12578
75129 12579
75621 12765
86150 15086
88165 15865
91465 16549
91769 16679
96617 17969
98711 18197
99481 18499
Java
import java.util.BitSet;
public final class NumbersWithTheSameDigitSetInBase10AndBase16 {
public static void main(String[] args) {
for ( int i = 0, count = 0; i < 100_000; i++ ) {
if ( digitSet(i, 10).equals(digitSet(i, 16)) ) {
System.out.print(String.format("%5d%s", i, ( count++ % 10 == 9 ? "\n" : " " )));
}
}
System.out.println();
}
private static BitSet digitSet(int number, int base) {
BitSet digitSet = new BitSet();
while ( number > 0 ) {
digitSet.set(number % base);
number /= base;
}
return digitSet;
}
}
- Output:
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
JavaScript
(() => {
"use strict";
const main = () => [
"(dec, hex)",
...enumFromTo(1)(100000).flatMap(n => {
const
d = n.toString(10),
h = n.toString(16);
return eqSet(new Set(d))(
new Set(h)
) ? [
`(${d}, ${h})`
] : [];
})
].join("\n");
// --------------------- GENERIC ---------------------
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
n => Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// eqSet :: Set a -> Set a -> Bool
const eqSet = a =>
// True if the two sets have
// the same size and members.
b => a.size === b.size && (
Array.from(a).every(x => b.has(x))
);
// MAIN ---
return main();
})();
- Output:
(dec, hex) (1, 1) (2, 2) (3, 3) (4, 4) (5, 5) (6, 6) (7, 7) (8, 8) (9, 9) (53, 35) (371, 173) (913, 391) (1040, 410) (2080, 820) (2339, 923) (4100, 1004) (5141, 1415) (5412, 1524) (5441, 1541) (6182, 1826) (8200, 2008) (9241, 2419) (13593, 3519) (13665, 3561) (13969, 3691) (16406, 4016) (20530, 5032) (26946, 6942) (30979, 7903) (32803, 8023) (33638, 8366) (33840, 8430) (33841, 8431) (33842, 8432) (33843, 8433) (33844, 8434) (33845, 8435) (33846, 8436) (33847, 8437) (33848, 8438) (33849, 8439) (34883, 8843) (37943, 9437) (38931, 9813) (38966, 9836) (38995, 9853) (66310, 10306) (71444, 11714) (71497, 11749) (71511, 11757) (75120, 12570) (75121, 12571) (75122, 12572) (75123, 12573) (75124, 12574) (75125, 12575) (75126, 12576) (75127, 12577) (75128, 12578) (75129, 12579) (75621, 12765) (86150, 15086) (88165, 15865) (91465, 16549) (91769, 16679) (96617, 17969) (98711, 18197) (99481, 18499)
jq
Also works with gojq, the Go implementation of jq.
Also works with fq, a Go implementation of a large subset of 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." ) )
- Output:
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.
Julia
dhstring(N) = [i for i in 0:N if sort(unique(digits(i))) == sort(unique(digits(i, base=16)))]
foreach(p -> print(rpad(p[2], 6), p[1] % 10 == 0 ? "\n" : ""), enumerate(dhstring(100000)))
- Output:
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
Kotlin
fun main() {
(0..99999).asSequence()
.filter { it.toString(10).toSet() == it.toString(16).toSet() }
.chunked(10) { it.joinToString(" ") { "%5d".format(it) } }
.forEach(::println)
}
- Output:
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
langur
# using hash keys to create a pseudo-set
val makeset = fn n:fold(split(n), by=fn h, v: h ~ {v: 0}, init={:})
var col = 1
for n in 0 .. 99999 {
if makeset(n) == makeset(string(n, fmt=16)) {
write "{{n:7}}"
if col div 10: writeln()
col += 1
}
}
writeln()
- Output:
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
Mathematica /Wolfram Language
ClearAll[Base1016SameDigitsQ]
Base1016SameDigitsQ[n_Integer] := Module[{id10, id16},
id10 = Union[IntegerDigits[n, 10]];
id16 = Union[IntegerDigits[n, 16]];
id10 === id16
]
Multicolumn[Select[Range[0, 10^5 - 1], Base1016SameDigitsQ], Appearance -> "Horizontal"]
- Output:
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
Nim
There are many ways to find the numbers. We chose to build directly the set of digits in base 10 and base 16 rather than using the string representations (more code but more efficient).
import strutils, sugar
const Lim = 99_999
type Digit = 0..15
func digitSet(n: Natural; b: Positive): set[Digit] =
## Return the set of digits of "n" written in base "b".
assert b <= 16
if n == 0: return {Digit 0}
var n = n
while n != 0:
result.incl n mod b
n = n div b
# Build the list of numbers.
let list = collect(newSeq):
for n in 0..Lim:
if n.digitSet(10) == n.digitSet(16): n
# Display result.
echo "Found $1 numbers less than $2:".format(list.len, insertSep($(Lim + 1)))
for i, n in list:
stdout.write ($n).align(5), if (i + 1) mod 10 == 0: '\n' else: ' '
echo()
- Output:
Found 69 numbers less than 100_000: 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
OCaml
module CSet = Set.Make(struct
type t = char
let compare = compare
end)
let str2cset str = CSet.add_seq (String.to_seq str) CSet.empty
let has_same_digits n =
let deci = Format.sprintf "%d" n in
let hexa = Format.sprintf "%x" n in
(* Don't use '=' to compare sets, it only returns true
for sets that are structurally the same, (same elements added in same order)
use CSet.equal to check they have the same elements *)
CSet.equal (str2cset deci) (str2cset hexa)
let rec list_similar ?(acc=[]) n =
if n < 0 then acc
else if has_same_digits n then list_similar ~acc:(n::acc) (n-1)
else list_similar ~acc (n-1)
let () =
let same_digits = list_similar 100_000 in
List.iteri (fun i x ->
Format.printf "%6d:%#8x%s" x x (if i mod 6 = 5 then "\n" else " ")) same_digits;
print_newline ()
- Output:
0: 0 1: 0x1 2: 0x2 3: 0x3 4: 0x4 5: 0x5 6: 0x6 7: 0x7 8: 0x8 9: 0x9 53: 0x35 371: 0x173 913: 0x391 1040: 0x410 2080: 0x820 2339: 0x923 4100: 0x1004 5141: 0x1415 5412: 0x1524 5441: 0x1541 6182: 0x1826 8200: 0x2008 9241: 0x2419 13593: 0x3519 13665: 0x3561 13969: 0x3691 16406: 0x4016 20530: 0x5032 26946: 0x6942 30979: 0x7903 32803: 0x8023 33638: 0x8366 33840: 0x8430 33841: 0x8431 33842: 0x8432 33843: 0x8433 33844: 0x8434 33845: 0x8435 33846: 0x8436 33847: 0x8437 33848: 0x8438 33849: 0x8439 34883: 0x8843 37943: 0x9437 38931: 0x9813 38966: 0x9836 38995: 0x9853 66310: 0x10306 71444: 0x11714 71497: 0x11749 71511: 0x11757 75120: 0x12570 75121: 0x12571 75122: 0x12572 75123: 0x12573 75124: 0x12574 75125: 0x12575 75126: 0x12576 75127: 0x12577 75128: 0x12578 75129: 0x12579 75621: 0x12765 86150: 0x15086 88165: 0x15865 91465: 0x16549 91769: 0x16679 96617: 0x17969 98711: 0x18197 99481: 0x18499
Pascal
simply adding 1 to a number and convert as if hex digits.
program Dec_Hex_same_UsedDigits;
{$IFDEF FPC}
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
const
UpperLimit = 100*1000;//1000*1000*1000
type
tUsedDigits = array[0..15] of byte;
var
FormCnt: Int32;
UsedDigits : tUsedDigits;
procedure Out_(n,h:Uint32);
Begin
write(n:11,':',h:7);
inc(FormCnt);
If FormCnt >= 4 then
Begin
FormCnt := 0;
writeln;
end;
end;
function DecHexToDec(n:Uint32):UInt32;
//convert n as its decimal digits are a hex number
//marking used Digits
var
pD : pUint64;
q,r,pot16 : UInt32;
begin
//clear UsedDigits
//For q := Low(UsedDigits) to High(UsedDigits) do UsedDigits[q] := 0;
//much faster
pD := @UsedDigits;pD[0]:= 0;pD[1]:= 0;
result := 0;
pot16 := 0;
repeat
q := n Div 10;
r := n - 10* q;//n mod 10
//marked by hex
UsedDigits[r] := 2;
result := result+ r shl pot16;
inc(pot16,4);
n := q;
until n = 0;
end;
var
DecHex,n:Uint32;
i,q,r,count : Uint32;
Begin
FormCnt := 0;
count := 0;
DecHex := 0;
repeat
n := DecHexToDec(DecHex);
if n > UpperLimit then
break;
//check UsedDigits
i := n;
repeat
q := i Div 10;
r := i - 10* q;
//if unused digit then break
if UsedDigits[r] = 0 then
BREAK;
//marked by dec
UsedDigits[r] := 1;
i := q;
until i = 0;
if i = 0 then
Begin
repeat
//was marked only by hex
if UsedDigits[i]>1 then
break;
inc(i);
until i > 9;
if i > 9 then
Begin
if n< 100*1000 then
Out_(n,DecHex);
inc(count);
end;
end;
inc(DecHex);
until false;
writeln;
writeln('count : ',count);
END.
- Output:
TIO.RUN 0: 0 1: 1 2: 2 3: 3 4: 4 5: 5 6: 6 7: 7 8: 8 9: 9 53: 35 371: 173 913: 391 1040: 410 2080: 820 2339: 923 4100: 1004 5141: 1415 5412: 1524 5441: 1541 6182: 1826 8200: 2008 9241: 2419 13593: 3519 13665: 3561 13969: 3691 16406: 4016 20530: 5032 26946: 6942 30979: 7903 32803: 8023 33638: 8366 33840: 8430 33841: 8431 33842: 8432 33843: 8433 33844: 8434 33845: 8435 33846: 8436 33847: 8437 33848: 8438 33849: 8439 34883: 8843 37943: 9437 38931: 9813 38966: 9836 38995: 9853 66310: 10306 71444: 11714 71497: 11749 71511: 11757 75120: 12570 75121: 12571 75122: 12572 75123: 12573 75124: 12574 75125: 12575 75126: 12576 75127: 12577 75128: 12578 75129: 12579 75621: 12765 86150: 15086 88165: 15865 91465: 16549 91769: 16679 96617: 17969 98711: 18197 99481: 18499 count : 69 //1000*1000*1000 966366598:39999586 966366853:39999685 count : 54558 Real time: 1.666 s
Perl
use strict;
no warnings;
use feature 'say';
sub eqv { (join '', sort split '', $_[0]) eq (join '', sort split '', $_[1]) }
say join ' ', grep { eqv $_, sprintf '%x', $_ } 1..100_000;
- Output:
1 2 3 4 5 6 7 8 9 53 371 913 4100 5141 5412 6182 8200 9241 75120 75121 75122 75123 75124 75125 75126 75127 75128 75129 75621 86150 91465 98711 99481
Phix
function handusc(integer n) return unique(sprintf("%x",n))=unique(sprintf("%d",n)) end function ?shorten(filter(tagset(100000,0),handusc),"found",10)
- Output:
{0,1,2,3,4,5,6,7,8,9,"...",75128,75129,75621,86150,88165,91465,91769,96617,98711,99481," (69 found)"}
Python
Procedural
col = 0
for i in range(100000):
if set(str(i)) == set(hex(i)[2:]):
col += 1
print("{:7}".format(i), end='\n'[:col % 10 == 0])
print()
- Output:
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
Functional
'''Decimal - Hexadecimal numbers'''
# p :: Int -> Bool
def p(n):
'''True if the hex and dec representations
of n use the same set of digits.
'''
return set(hex(n)[2:]) == set(str(n))
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Matches below 100000'''
print(
table(10)([
str(x) for x in range(100000)
if p(x)
])
)
# ----------------------- GENERIC ------------------------
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divisible, the final list will be shorter than n.
'''
def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
# table :: Int -> [a] -> String
def table(n):
'''A list of values formatted as
right-justified rows of n columns.
'''
def go(xs):
w = len(xs[-1])
return '\n'.join(
' '.join(row) for row in chunksOf(n)([
x.rjust(w, ' ') for x in xs
])
)
return go
# MAIN ---
if __name__ == '__main__':
main()
- Output:
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
or in terms of a list comprehension:
for nh in ([
(n, h) for n in range(0, 1 + 100000)
if (
(h := hex(n)[2:])
and set(str(n)) == set(h)
)
]):
print(nh)
(0, '0') (1, '1') (2, '2') (3, '3') (4, '4') (5, '5') (6, '6') (7, '7') (8, '8') (9, '9') (53, '35') (371, '173') (913, '391') (1040, '410') (2080, '820') (2339, '923') (4100, '1004') (5141, '1415') (5412, '1524') (5441, '1541') (6182, '1826') (8200, '2008') (9241, '2419') (13593, '3519') (13665, '3561') (13969, '3691') (16406, '4016') (20530, '5032') (26946, '6942') (30979, '7903') (32803, '8023') (33638, '8366') (33840, '8430') (33841, '8431') (33842, '8432') (33843, '8433') (33844, '8434') (33845, '8435') (33846, '8436') (33847, '8437') (33848, '8438') (33849, '8439') (34883, '8843') (37943, '9437') (38931, '9813') (38966, '9836') (38995, '9853') (66310, '10306') (71444, '11714') (71497, '11749') (71511, '11757') (75120, '12570') (75121, '12571') (75122, '12572') (75123, '12573') (75124, '12574') (75125, '12575') (75126, '12576') (75127, '12577') (75128, '12578') (75129, '12579') (75621, '12765') (86150, '15086') (88165, '15865') (91465, '16549') (91769, '16679') (96617, '17969') (98711, '18197') (99481, '18499')
Quackery
[ 0 swap witheach [ bit | ] ] is ->set ( [ --> s )
[ 10 base put
dup number$ ->set
16 base replace
swap number$ ->set
base release
= ] is dec=hex ( n --> b )
[]
100000 times
[ i^ dec=hex if
[ i^ number$
nested join ] ]
60 wrap$
- Output:
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
Raku
Only bother to check numbers that could possibly match.
Several different interpretations of the (originally very vague) task instructions. It has now been clarified to mean the second.
say "Numbers up to 100,000 which when expressed in hexadecimal and in decimal
are composed of the same digit glyphs. (All the same glyphs, all the same
quantity.)\n";
display-numbers calculated-with &infix:<eqv>, <Bag>;
say "\nNumbers up to 100,000 which when expressed in hexadecimal and in decimal
are composed from the same digit glyphs. (All the same glyphs are present,
possibly different quantity.)\n";
display-numbers calculated-with &infix:<eqv>, <Set>;
say "\nNumbers up to 100,000 which, when expressed in hexadecimal use glyphs
that are a subset of those used when expressed in decimal. (Every glyph in
decimal is present in hexadecimal the same or fewer (possibly zero) times)\n";
display-numbers calculated-with &infix:<⊆>, <Bag>;
say "\nNumbers up to 100,000 which, when expressed in hexadecimal use glyphs
that are a subset of those used when expressed in decimal. (Every glyph in
decimal is present in hexadecimal in some quantity, possibly zero, possibly more
than in decimal)\n";
display-numbers calculated-with &infix:<⊆>, <Set>;
sub display-numbers ($_) { say .elems ~ " found:\n" ~ .batch(20)».fmt('%5d').join: "\n" }
sub calculated-with ( &op, $container ) {
cache ^18699 .map( {:16(.Str)} ).hyper(:1000batch).grep( {
reduce( &op, (.fmt('%x'), $_).map: { .comb."$container"() } )
} )
}
- Output:
Numbers which when expressed in hexadecimal and in decimal are composed of the same digit glyphs. (All the same glyphs, all the same quantity.) 34 found: 0 1 2 3 4 5 6 7 8 9 53 371 913 4100 5141 5412 6182 8200 9241 75120 75121 75122 75123 75124 75125 75126 75127 75128 75129 75621 86150 91465 98711 99481 Numbers which when expressed in hexadecimal and in decimal are composed from the same digit glyphs. (All the same glyphs are present, possibly different quantity.) 69 found: 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 Numbers which, when expressed in hexadecimal use glyphs that are a subset of those used when expressed in decimal. (Every glyph in decimal is present in hexadecimal the same or fewer (possibly zero) times) 141 found: 0 1 2 3 4 5 6 7 8 9 53 371 913 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1345 1937 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2339 4100 5141 5412 6182 8200 9241 12306 13593 13665 13969 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16743 18453 20513 20530 20546 21025 25169 25896 25986 26370 26738 26913 26930 26946 29217 29714 30768 30784 30979 32800 32801 32802 32803 32804 32805 32806 32807 32808 32809 32850 33589 33840 33841 33842 33843 33844 33845 33846 33847 33848 33849 34868 34880 34881 34882 34883 34884 34885 34886 34887 34888 34889 36902 36950 37492 37943 38931 38960 38961 38962 38963 38964 38965 38966 38967 38968 38969 38984 38995 39298 75120 75121 75122 75123 75124 75125 75126 75127 75128 75129 75621 86150 91465 98711 99481 Numbers which, when expressed in hexadecimal use glyphs that are a subset of those used when expressed in decimal. (Every glyph in decimal is present in hexadecimal in some quantity, possibly zero, possibly more than in decimal) 514 found: 0 1 2 3 4 5 6 7 8 9 17 53 85 371 853 913 1024 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1345 1365 1604 1633 1635 1638 1689 1937 2048 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2178 2184 2339 4100 4160 4371 4417 4471 4481 4721 4913 4931 5140 5141 5412 5441 5461 6168 6182 8200 8210 8320 8721 9241 9284 9523 9625 9762 9826 9862 10248 10258 10280 10529 12304 12305 12306 12307 12320 12340 12563 12593 12643 12803 12833 12834 12835 12853 12953 13056 13057 13059 13062 13072 13073 13075 13079 13080 13104 13105 13107 13124 13171 13175 13185 13187 13209 13364 13384 13427 13448 13459 13585 13587 13589 13593 13651 13653 13654 13665 13846 13862 13926 13969 14385 14387 14388 14739 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16420 16480 16640 16704 16740 16742 16743 17408 17409 17425 17426 17428 17431 17442 17463 17473 17476 17479 17480 17495 17524 17745 18247 18440 18449 18452 18453 18456 18472 18483 18497 18504 18564 20485 20501 20512 20513 20514 20517 20530 20531 20533 20544 20546 20548 20549 20560 20561 20562 20563 20564 20565 20566 20567 20568 20569 20576 20597 21025 21395 21504 21524 21540 21541 21845 21905 22357 22568 22658 24608 24610 24630 24640 24680 25106 25126 25169 25186 25361 25604 25634 25684 25896 25961 25986 26129 26130 26131 26134 26145 26146 26148 26150 26163 26184 26208 26210 26214 26248 26370 26407 26470 26471 26487 26658 26728 26738 26758 26913 26914 26918 26921 26930 26931 26934 26937 26946 26948 26965 26978 26982 26984 26985 28704 28706 28740 28807 29217 29337 29714 29764 29847 30467 30471 30517 30547 30567 30576 30579 30728 30768 30784 30976 30979 31097 32800 32801 32802 32803 32804 32805 32806 32807 32808 32809 32840 32850 33280 33587 33589 33638 33689 33840 33841 33842 33843 33844 33845 33846 33847 33848 33849 33864 34678 34820 34852 34867 34868 34871 34872 34880 34881 34882 34883 34884 34885 34886 34887 34888 34889 34948 34968 36902 36950 36960 37139 37169 37492 37529 37689 37779 37924 37940 37943 37945 38195 38791 38792 38793 38920 38931 38952 38960 38961 38962 38963 38964 38965 38966 38967 38968 38969 38984 38995 39048 39185 39187 39193 39200 39201 39202 39203 39204 39205 39206 39207 39208 39209 39217 39218 39219 39225 39234 39241 39250 39251 39253 39257 39266 39267 39287 39298 39313 39315 39321 66310 67140 67190 69910 69913 69921 70001 70162 70513 71025 71206 71265 71266 71286 71440 71441 71442 71443 71444 71445 71446 71447 71448 71449 71462 71473 71489 71492 71495 71497 71504 71505 71509 71511 71526 71537 71539 71540 71541 71543 71569 71680 71748 71800 71808 71809 71815 71816 71940 71953 71957 71959 71961 71993 74129 75120 75121 75122 75123 75124 75125 75126 75127 75128 75129 75621 76129 78103 78131 78135 78136 78193 79153 79731 81940 82194 82241 82471 82514 84100 84104 86150 87315 87351 87415 88145 88152 88165 90163 91465 91665 91670 91671 91673 91682 91760 91761 91762 91763 91764 91765 91766 91767 91768 91769 92561 94617 95761 96017 96617 98581 98711 98712 98713 99481
REXX
/*REXX pgm finds integers when shown in decimal and hexadecimal use the same numerals.*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n = 100000 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 10 /*width of a number in any column. */
@hHex= ' decimal integers when displayed in decimal and' ,
"hexadecimal use the same numerals, where N < " commas(n)
say ' index │'center(@hHex, 1 + cols*(w+1) ) /*display the title for the output. */
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */
dHex= 0; idx= 1 /*initialize # of high hexadecimal nums*/
$= /*list of high hexadecimal #'s (so far)*/
do j=0 for n; h= d2x(j) /*search for high hexadecimal numbers. */
if verify(j, h)>0 then iterate /*Does the decimal and hexadecimal ··· */ /* ◄■■■■■■■■ a filter. */
if verify(h, j)>0 then iterate /* ··· versions use same numerals? */ /* ◄■■■■■■■■ a filter. */
dHex= dHex + 1 /*bump number of decimal-hex numbers. */
$= $ right(commas(j), w) /*add a dec-hexadecimal number──► list.*/
if dHex // cols \== 0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─') /*display the foot sep for output. */
say
say 'Found ' commas(dHex) @hHex
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
- output when using the default inputs:
index │ decimal integers when displayed in decimal and hexadecimal use the same numerals, where N < 100,000 ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 0 1 2 3 4 5 6 7 8 9 11 │ 53 371 913 1,040 2,080 2,339 4,100 5,141 5,412 5,441 21 │ 6,182 8,200 9,241 13,593 13,665 13,969 16,406 20,530 26,946 30,979 31 │ 32,803 33,638 33,840 33,841 33,842 33,843 33,844 33,845 33,846 33,847 41 │ 33,848 33,849 34,883 37,943 38,931 38,966 38,995 66,310 71,444 71,497 51 │ 71,511 75,120 75,121 75,122 75,123 75,124 75,125 75,126 75,127 75,128 61 │ 75,129 75,621 86,150 88,165 91,465 91,769 96,617 98,711 99,481 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── Found 69 decimal integers when displayed in decimal and hexadecimal use the same numerals, where N < 100,000
Ring
see "working..." + nl
row = 0
limit = 100000
for n = 0 to limit
flag1 = 1
flag2 = 1
decStr = string(n)
hexStr = hex(n)
for m = 1 to len(decStr)
ind = substr(hexStr,decStr[m])
if ind < 1
flag1 = 0
exit
ok
next
for p = 1 to len(hexStr)
ind = substr(decStr,hexStr[p])
if ind < 1
flag2 = 0
exit
ok
next
if flag1 = 1 and flag2 = 1
row = row + 1
see "" + n + " "
if row%5 = 0
see nl
ok
ok
next
see nl + "Found " + row + " numbers" + nl
see "done..." + nl
- Output:
working... 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 Found 69 numbers done...
RPL
« → b « #0 WHILE OVER REPEAT SWAP b IDIV2 ROT SWAP ALOG R→B OR END NIP » » 'DIGSET' STO « { } 0 99999 FOR n IF n 10 DIGSET n 16 DIGSET == THEN n + END NEXT » 'TASK' STO
- Output:
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 }
Ruby
p (1..100_000).select{|n| n.digits.to_set == n.digits(16).to_set}
- Output:
[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]
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;
- Output:
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
Sidef
Simple solution:
^1e5 -> grep { Set(.digits...) == Set(.digits(16)...) }.say
Recursively generate the numbers (2x faster):
func generate_from_prefix(limit, p, base, digits) {
var seq = [p]
for d in (digits) {
var t = [d, p...]
if (t.digits2num(base) <= limit) {
seq << __FUNC__(limit, t, base, digits)...
}
}
return seq
}
func numbers_with_same_digitset(limit, base = 10) {
(1..9).map {|p| generate_from_prefix(limit, [p], base, @(0..9))... }\
.map {|t| digits2num(t, base) }\
.grep {|t| Set(t.digits...) == Set(t.digits(base)...) }\
.sort\
.prepend(0)
}
say numbers_with_same_digitset(1e5, 16)
- Output:
[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]
Wren
import "./fmt" for Conv, Fmt
import "./set" for Set
var limit = 1e5
var count = 0
System.print("Numbers under 100,000 which use the same digits in decimal or hex:")
for (n in 0...limit) {
var h = Conv.hex(n)
var hs = Set.new(h)
var ns = Set.new(n.toString)
if (hs == ns) {
count = count + 1
Fmt.write("$,6d ", n)
if (count % 10 == 0) System.print()
}
}
System.print("\n\n%(count) such numbers found.")
- Output:
Numbers under 100,000 which use the same digits in decimal or hex: 0 1 2 3 4 5 6 7 8 9 53 371 913 1,040 2,080 2,339 4,100 5,141 5,412 5,441 6,182 8,200 9,241 13,593 13,665 13,969 16,406 20,530 26,946 30,979 32,803 33,638 33,840 33,841 33,842 33,843 33,844 33,845 33,846 33,847 33,848 33,849 34,883 37,943 38,931 38,966 38,995 66,310 71,444 71,497 71,511 75,120 75,121 75,122 75,123 75,124 75,125 75,126 75,127 75,128 75,129 75,621 86,150 88,165 91,465 91,769 96,617 98,711 99,481 69 such numbers found.
XPL0
func DigitSet(N, D);
\Return a bit array containing the set of digits in N divisible by D
int N, D;
int Set;
[Set:= 0;
repeat N:= N/D;
Set:= Set or 1<<rem(0);
until N=0;
return Set;
];
int Count, N;
[Count:= 0;
for N:= 0 to 99999 do
[if DigitSet(N,10) = DigitSet(N,16) then
[IntOut(0, N);
Count:= Count+1;
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
];
CrLf(0); IntOut(0, Count); Text(0, " such numbers found.
")
]
- Output:
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.
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) << @as(u5, @truncate(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});
}
}
}
const std = @import("std");
const print = std.debug.print;
const LIMIT = 100_000;
pub fn main() void {
var count: usize = 0;
var i: u64 = 0;
while (i < LIMIT) : (i += 1)
if (calcDigitSet(i, 10).eql(calcDigitSet(i, 16))) {
count += 1;
print("{d:5}{c}", .{ i, @as(u8, if (count % 10 != 0) ' ' else '\n') });
};
}
fn calcDigitSet(n_: u64, comptime base: u5) std.StaticBitSet(16) {
if (base < 2 or base > 16)
@compileError("calcDigitSet() base must be in the closed interval 2 to 16");
var n = n_;
var set = std.StaticBitSet(16).initEmpty();
while (n != 0) : (n /= base)
set.set(n % base);
return set;
}