Numbers with same digit set in base 10 and base 16: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Wren)
Line 62: Line 62:
Found 69 numbers
Found 69 numbers
done...
done...
</pre>

=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-set}}
<lang ecmascript>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.")</lang>

{{out}}
<pre>
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.
</pre>
</pre>

Revision as of 09:49, 6 June 2021

Numbers with same digit set in base 10 and base 16 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 decimal numbers n that when converted to hexadecimal produce a number that uses the same digits as the original number, where n < 100000



Ring

<lang 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 </lang>

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...

Wren

Library: Wren-fmt
Library: Wren-set

<lang ecmascript>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.")</lang>

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.