Base-16 representation: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|REXX}}: added the computer programming language REXX.)
(Add Factor)
Line 52: Line 52:
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
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
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
500

301 such numbers found.
</pre>

=={{header|Factor}}==
The <code>non-decimal?</code> word is a translation of C++'s <code>nondecimal</code> function. Having multiple exit points in a word is not very efficient (yet?) in Factor, so I've tweaked it to have one exit point at the end.

{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: combinators.short-circuit formatting grouping io kernel
lists lists.lazy math prettyprint sequences ;

! Returns t if the hexadecimal representation of n contains a
! non-decimal digit.
: non-decimal? ( n -- ? )
[ dup { [ 0xF bitand 10 < ] [ 0 > ] } 1&& ]
[ -4 shift ] while zero? not ;

1 lfrom [ non-decimal? ] lfilter [ 501 < ] lwhile
list>array dup 15 group [ [ "%3d " printf ] each nl ] each nl
length pprint " such numbers found." print</lang>
{{out}}
<pre>
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
500



Revision as of 21:18, 2 June 2021

Base-16 representation 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

Show in decimal notation all positive integers (less than 501) which, when converted to hexadecimal notation, cannot be written without using at least one non-decimal digit ('a' to 'f').

C++

<lang cpp>#include <iomanip>

  1. include <iostream>

// Returns true if the hexadecimal representation of n contains at least one // non-decimal digit. bool nondecimal(unsigned int n) {

   for (; n > 0; n >>= 4) {
       if ((n & 0xF) > 9)
           return true;
   }
   return false;

}

int main() {

   unsigned int count = 0;
   for (unsigned int n = 0; n < 501; ++n) {
       if (nondecimal(n)) {
           ++count;
           std::cout << std::setw(3) << n << (count % 15 == 0 ? '\n' : ' ');
       }
   }
   std::cout << "\n\n" << count << " such numbers found.\n";

}</lang>

Output:
 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 

301 such numbers found.

Factor

The non-decimal? word is a translation of C++'s nondecimal function. Having multiple exit points in a word is not very efficient (yet?) in Factor, so I've tweaked it to have one exit point at the end.

Works with: Factor version 0.99 2021-02-05

<lang factor>USING: combinators.short-circuit formatting grouping io kernel lists lists.lazy math prettyprint sequences ;

! Returns t if the hexadecimal representation of n contains a ! non-decimal digit.

non-decimal? ( n -- ? )
   [ dup { [ 0xF bitand 10 < ] [ 0 > ] } 1&& ]
   [ -4 shift ] while zero? not ;

1 lfrom [ non-decimal? ] lfilter [ 501 < ] lwhile list>array dup 15 group [ [ "%3d " printf ] each nl ] each nl length pprint " such numbers found." print</lang>

Output:
 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 

301 such numbers found.

Raku

Yet another poorly specced, poorly named, trivial task.

How many integers in base 16 cannot be written without using a hexadecimal digit? All of them. Or none of them.

Base 16 is not hexadecimal. Hexadecimal is an implementation of base 16.

<lang perl6>use Base::Any; set-digits <⑩ ⑪ ⑫ ⑬ ⑭ ⑮ ⑯ ⑰ ⑱ ⑲ ⑳ ㉑ ㉒ ㉓ ㉔ ㉕>; say (7**35).&to-base(16);

  1. ⑭㉒⑱⑩⑰⑰⑳⑮⑱⑳⑩⑳⑱㉒㉑⑰㉒⑫⑭⑲⑯⑩㉔⑮⑰</lang>

How many of those glyphs are decimal digits? And yet it is in base 16, albeit with non-standard digit glyphs. So they all can be written without using a hexadecimal digit.

But wait a minute; is 2 a hexadecimal digit? Why yes, yes it is. So none of them can be written in hexadecimal without using a hexadecimal digit.


Bah. Show which when written in base 16, contain a digit glyph with a value greater than 9:

<lang perl6>say (0..500).grep( { any |.map: { .polymod(16 xx *) »>» 9 } } ).batch(20)».fmt('%3d').join: "\n";</lang>

Output:
 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


REXX

REXX automatically uses only uppercase when converting integers to hexadecimal,   but the lowercase alphabetic letters where all included for boilerplate code. <lang rexx>/*REXX pgm finds positive integers when shown in hexadecimal require an alphabetic glyph*/ parse arg n cols . /*obtain optional argument from the CL.*/ if n== | n=="," then n = 501 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 10 /*width of a number in any column. */

                    @hHex= ' integers when displayed in hexadecimal that require an'  ,
                           "alphabetical glyph to be shown, where  N < "     n

say ' index │'center(@hHex, 1 + cols*(w+1) ) /*display the title for the output. */ say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */ hHex= 0; y= 'abcdefABCDEF'; idx= 1 /*initialize # of high hexadecimal nums*/ $= /*list of high hexadecimal #'s (so far)*/

   do j=1  for n-1                              /*search for high hexadecimal numbers. */
   if verify(y, d2x(j), 'M')==0  then iterate   /*No alphabetical characters? Then skip*/
   hHex= hHex + 1                               /*bump number of high hexadecimal #'s. */
   $= $  right(j, w)                            /*add a high hexadecimal number──► list*/
   if hHex // 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 ' hHex @hHex exit 0 /*stick a fork in it, we're all done. */</lang>

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

Found  301  integers when displayed in hexadecimal that require an alphabetical glyph to be shown, where  N <  501

Ring

<lang ring> see "working..." + nl baseList = ["a","b","c","d","e","f"] row = 1 limit = 500

for n = 1 to limit

   num = 0
   flag = 1
   hex = hex(n)
   lenHex = len(hex)
   for m = 1 to lenHex        
       ind = find(baseList,hex[m])
       if ind < 1
          num = num + 1
       ok
   next
   if num != lenHex
      row = row + 1
      see "" + n + " "
      if row%10 = 0
         see nl
      ok
   ok

next

see nl + "done..." + nl </lang>

Output:
working...
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 
done...

Wren

Library: Wren-fmt

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

var nondecimal = "abcdef" var c = 0 for (i in 0..500) {

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

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

Output:
 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 

301 such numbers found.

XPL0

Borrowed masking concept from C++, which was much more elegant than my first solution. <lang XPL0>func HasHex(N); int N; [while N do

    [if (N&$F) > 9 then return true;  N:= N>>4];

return false; ];

int N, Cnt; [Cnt:= 0; for N:= 1 to 500 do

   [if HasHex(N) then
       [if N<100 then ChOut(0, ^ );
       IntOut(0, N);
       Cnt:= Cnt+1;
       if rem(Cnt/20) = 0 then CrLf(0) else ChOut(0, ^ );
       ];
   ];

CrLf(0); IntOut(0, Cnt); Text(0, " such numbers found."); CrLf(0); ]</lang>

Output:
 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 
301 such numbers found.