Numbers in base-16 representation that cannot be written with decimal digits: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Wren}}: Iteration now starts at 1.)
m (→‎{{header|Raku}}: trap some edge conditions I overlooked)
Line 83: Line 83:
#Generate such numbers directly, up to a threshold:
#Generate such numbers directly, up to a threshold:
put "\nGenerate: first {+$_}:\n", .batch(10)».map({ "{$_}({:16($_)})" })».fmt('%9s').join("\n") given
put "\nGenerate: first {+$_}:\n", .batch(10)».map({ "{$_}({:16($_)})" })».fmt('%9s').join("\n") given
((1..^Inf).grep(* % 7).map: { .base(7).trans: [1..6] => ['A'..'F'] } )[^42];
((1..^Inf).grep(* % 7).map( { .base(7).trans: [1..6] => ['A'..'F'] } )).grep(!*.contains: 0)[^42];


#Count such numbers directly, up to a threshold
#Count such numbers directly, up to a threshold
my $upto = 500;
my $upto = 500;
put "\nCount: " ~ [+] map {exp($_, 6)}, 1..($upto.log(16).floor);</lang>
put "\nCount: " ~ [+] flat (map {exp($_, 6)}, 1..($upto.log(16).floor)),
+(exp($upto.log(16).floor, 16) .. $upto).grep( { so all |.map: { .polymod(16 xx *) »>» 9 } });</lang>
{{out}}
{{out}}
<pre>Filter: 42 such numbers:
<pre>Filter: 42 such numbers:

Revision as of 13:02, 24 June 2021

Numbers in base-16 representation that cannot be written with decimal digits 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 positive integers in base-16 representation that cannot be written with decimal digits,   where   n   <   50010

Go

<lang go>package main

import (

   "fmt"
   "strconv"
   "strings"

)

func main() {

   const decimal = "0123456789"
   c := 0
   for i := int64(1); i < 500; i++ {
       hex := strconv.FormatInt(i, 16)
       if !strings.ContainsAny(decimal, hex) {
           fmt.Printf("%3d ", i)
           c++
           if c%14 == 0 {
               fmt.Println()
           }
       }
   }
   fmt.Printf("\n%d such numbers found.\n", c)

}</lang>

Output:
 10  11  12  13  14  15 170 171 172 173 174 175 186 187 
188 189 190 191 202 203 204 205 206 207 218 219 220 221 
222 223 234 235 236 237 238 239 250 251 252 253 254 255 

42 such numbers found.

Raku

Find numbers in base-16 representation that cannot be written with decimal digits, where n < 500

This is literally the exact same task as (the horribly named) Base-16 representation task.

Leaving aside the requirement that it be base 16 (well, base negative 16 according to the task title); assume it really means hexadecimal, otherwise all bets are off.

I challenge anyone to demonstrate how, say 46510, can be written in hexadecimal using only decimal digits.

The task as written:

<lang perl6>put "{+$_} such numbers:\n", .batch(20)».fmt('%3d').join("\n")

   given (1..500).grep( { so any |.map: { .polymod(16 xx *) »>» 9 } } );</lang>
Output:
301 such numbers:
 10  11  12  13  14  15  26  27  28  29  30  31  42  43  44  45  46  47  58  59
 60  61  62  63  74  75  76  77  78  79  90  91  92  93  94  95 106 107 108 109
110 111 122 123 124 125 126 127 138 139 140 141 142 143 154 155 156 157 158 159
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 266 267 268 269
270 271 282 283 284 285 286 287 298 299 300 301 302 303 314 315 316 317 318 319
330 331 332 333 334 335 346 347 348 349 350 351 362 363 364 365 366 367 378 379
380 381 382 383 394 395 396 397 398 399 410 411 412 413 414 415 416 417 418 419
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
500


What the task author probably meant.

Find numbers in decimal that when written in hexadecimal are expressed using only alphabetic glyphs.

Which is a tiny (2 character) change from Base-16 representation. Add some other (possibly useful) functionality.

<lang perl6>#Filter out such numbers from a range: put "Filter: {+$_} such numbers:\n", .batch(20)».fmt('%3d').join("\n")

   given (1..500).grep( { so all |.map: { .polymod(16 xx *) »>» 9 } } );
  1. Generate such numbers directly, up to a threshold:

put "\nGenerate: first {+$_}:\n", .batch(10)».map({ "{$_}({:16($_)})" })».fmt('%9s').join("\n") given

   ((1..^Inf).grep(* % 7).map( { .base(7).trans: [1..6] => ['A'..'F'] } )).grep(!*.contains: 0)[^42];
  1. Count such numbers directly, up to a threshold

my $upto = 500; put "\nCount: " ~ [+] flat (map {exp($_, 6)}, 1..($upto.log(16).floor)), +(exp($upto.log(16).floor, 16) .. $upto).grep( { so all |.map: { .polymod(16 xx *) »>» 9 } });</lang>

Output:
Filter: 42 such numbers:
 10  11  12  13  14  15 170 171 172 173 174 175 186 187 188 189 190 191 202 203
204 205 206 207 218 219 220 221 222 223 234 235 236 237 238 239 250 251 252 253
254 255

Generate: first 42:
    A(10)     B(11)     C(12)     D(13)     E(14)     F(15)   AA(170)   AB(171)   AC(172)   AD(173)
  AE(174)   AF(175)   BA(186)   BB(187)   BC(188)   BD(189)   BE(190)   BF(191)   CA(202)   CB(203)
  CC(204)   CD(205)   CE(206)   CF(207)   DA(218)   DB(219)   DC(220)   DD(221)   DE(222)   DF(223)
  EA(234)   EB(235)   EC(236)   ED(237)   EE(238)   EF(239)   FA(250)   FB(251)   FC(252)   FD(253)
  FE(254)   FF(255)

Count: 42

REXX

<lang rexx>/*REXX pgm finds positive integers when shown in hex that can't be written with dec digs*/ parse arg n cols . /*obtain optional argument from the CL.*/ if n== | n=="," then n = 500 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 10 /*width of a number in any column. */ title= " positive integers when shown in hexadecimal that can't be written with" ,

      'decimal digits,  where  N  < '    n

say ' index │'center(title, 1 + cols*(w+1) ) /*display the title for the output. */ say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */ found= 0; y= 0123456789; idx= 1 /*define a set of forbidden glyphs. */ $= /*list of numbers found (so far). */

   do j=1  for n-1                              /*find ints in hex with no dec. digits.*/
   if verify(y, d2x(j), 'M')\==0  then iterate  /*Any dec. digs in hex number?   Skip. */    /* ◄■■■■■■■■ the filter. */
   found= found + 1                             /*bump number of found such numbers.   */
   $= $  right(j, w)                            /*add the found number  ───►  $  list. */
   if found // 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 ' found title exit 0 /*stick a fork in it, we're all done. */</lang>

output   when using the default inputs:
 index │    positive integers when shown in hexadecimal that can't be written with decimal digits,  where  N  <  500
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │         10         11         12         13         14         15        170        171        172        173
  11   │        174        175        186        187        188        189        190        191        202        203
  21   │        204        205        206        207        218        219        220        221        222        223
  31   │        234        235        236        237        238        239        250        251        252        253
  41   │        254        255
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────

Found  42  positive integers when shown in hexadecimal that can't be written with decimal digits,  where  N  <  500

Ring

<lang ring> see "working..." + nl see "Numbers in base-16 representation that cannot be written with decimal digits:" + nl

row = 0 baseList = "ABCDEF" limit = 500

for n = 1 to limit

   flag = 1  
   hex = upper(hex(n))
   for m = 1 to len(hex)
       ind = substr(baseList,hex[m])
       if ind < 1
          flag = 0
          exit
       ok
   next
    
   if flag = 1
      see "" + n + " "
      row = row + 1
      if row%5 = 0
         see nl
      ok
   ok

next

see nl + "Found " + row + " numbers" + nl see "done..." + nl </lang>

Output:
working...
Numbers in base-16 representation that cannot be written with decimal digits:
10 11 12 13 14 
15 170 171 172 173 
174 175 186 187 188 
189 190 191 202 203 
204 205 206 207 218 
219 220 221 222 223 
234 235 236 237 238 
239 250 251 252 253 
254 255 
Found 42 numbers
done...

Wren

Library: Wren-fmt

<lang ecmascript>import "/fmt" for Conv, Fmt

var decimal = "0123456789" var c = 0 for (i in 1..499) {

   var hex = Conv.hex(i)
   if (!hex.any { |c| decimal.contains(c) }) {
       Fmt.write("$3s ", i)
       c = c + 1
       if (c % 14 == 0) System.print()
   }

} System.print("\n%(c) such numbers found.")</lang>

Output:
 10  11  12  13  14  15 170 171 172 173 174 175 186 187 
188 189 190 191 202 203 204 205 206 207 218 219 220 221 
222 223 234 235 236 237 238 239 250 251 252 253 254 255 

42 such numbers found.