Base 16 numbers needing a to f: Difference between revisions

(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(→‎BQN: add)
 
(13 intermediate revisions by 8 users not shown)
Line 616:
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic">10 DEFINT I,J
<syntaxhighlight lang="qbasic">function needs_af (n)
20 FOR I=1 TO 500
while n > 0
30 J=I
if (n % 16) > 9 then return true
40 IF (J AND 15)>=10 THEN PRINT I, ELSE J=J/16: IF J>9 THEN 40
n = n \ 16
end while
return false
end function
 
for i = 1 to 500
if needs_af(i) then print i; " ";
next i
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">function needs_af( byval n as uinteger ) as boolean
while n>0
if n mod 16 > 9 then return true
n\=16
wend
return false
end function
 
for i as uinteger = 1 to 500
if needs_af(i) then print i;" ";
next i
</syntaxhighlight>
{{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
</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
For i As Integer = 1 To 500
If needs_af(i) Then Print i; " ";
Next
End
 
Function needs_af(n As Integer) As Boolean
 
While n > 0
If n Mod 16 > 9 Then Return True
n \= 16
Wend
Return False
 
End Function</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|MSX_BASIC}}
<syntaxhighlight lang="gwbasic">10 DEFINT I,J
20 FOR I = 1 TO 500
30 J = I
40 IF (J AND 15) >= 10 THEN PRINT I, ELSE J = J / 16: IF J > 9 THEN 40
50 NEXT I</syntaxhighlight>
{{out}}
Line 684 ⟶ 742:
498 499 500</pre>
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|BASIC256QuickBASIC}}===
{{trans|Modula-2}}
<syntaxhighlight lang="qbasic">function needs_af (n)
<syntaxhighlight lang="qbasic">
while n > 0
REM Base 16 numbers needing A to F
if (n % 16) > 9 then return true
DECLARE FUNCTION UsesAToF! (BYVAL N%)
n = n \ 16
CONST TRUE = -1, FALSE = 0, MAX = 500
end while
Count% = 0
return false
FOR N% = 1 TO MAX
end function
IF UsesAToF(N%) THEN
PRINT USING "####"; N%;
Count% = Count% + 1
IF Count% MOD 20 = 0 THEN PRINT
END IF
NEXT
PRINT : PRINT
PRINT USING "### numbers found."; Count%
END
 
FUNCTION UsesAToF (BYVAL N%)
WHILE N% > 9
IF N% MOD 16 >= 10 THEN
UsesAToF = TRUE: EXIT FUNCTION
ELSE
N% = N% \ 16
END IF
WEND
UsesAToF = FALSE
END FUNCTION
</syntaxhighlight>
{{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
 
301 numbers found.
</pre>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="vb">for i = 1 to 500
if needsaf(i) then print i; " ";
next i
end
 
function needsaf(n)
while n > 0
if (n mod 16) > 9 then needsaf = 1 : goto [exit]
n = int(n / 16)
wend
needsaf = 0
[exit]
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|True BASIC}}===
{{trans|QuickBASIC}}
<syntaxhighlight lang="qbasic">FUNCTION usesatof(n)
DO WHILE n > 9
IF REMAINDER(n,16) >= 10 THEN
LET usesatof = True
EXIT FUNCTION
ELSE
LET n = IP(n/16)
END IF
LOOP
LET usesatof = False
END FUNCTION
 
LET True = -1
LET False = 0
LET max = 500
LET count = 0
FOR n = 1 TO ROUND(max)
IF usesatof(n)<>0 THEN
PRINT USING "####": n;
LET count = count+1
IF REMAINDER(count,20) = 0 THEN PRINT
END IF
NEXT n
PRINT
PRINT USING "### numbers found.": count
END</syntaxhighlight>
{{out}}
<pre>Same as QuickBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub needs_af (n)
while n > 0
if mod(n, 16) > 9 then return true : fi
n = int(n / 16)
wend
return false
end sub
 
for i = 1 to 500
if needs_af(i) then print i;, " ";, : fi
next i
end</syntaxhighlight>
 
 
=={{header|BCPL}}==
Line 735 ⟶ 894:
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
500</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">43‿7 ⥊ / 9 < 16 ⌊∘÷˜•_while_(≤ ∧ 9 ≥ |)¨ ↕501</syntaxhighlight>
{{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
┘</pre>
 
=={{header|C++}}==
Line 968 ⟶ 1,176:
490 491 492 493 494 495 496 497 498 499
500</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
An example of using Delphi-style operations using set operators to find hex nibbles A..F
<syntaxhighlight lang="Delphi">
function HasAlphaDigits(W: integer): boolean;
{test if number has A..F in one or more of the digits}
{Only works for numbers up to 9FF or 2559}
begin
Result:=((W and $000F) in [$000A,$000B,$000C,$000D,$000E,$000F]) or
((W and $00F0) in [$00A0,$00B0,$00C0,$00D0,$00E0,$00F0]);
end;
 
procedure HasNibblesAF(Memo: TMemo);
{Find all numbers 1 through 500 where the hex}
{version has A..F in any of the nibbles}
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=1 to 500 do
if HasAlphaDigits(I) then
begin
Inc(Cnt);
S:=S+Format('%4.0d', [I]);
if (Cnt mod 20)=0 then S:=S+#$0D+#$0A;
end;
Memo.Text:=S;
Memo.Lines.Add('Count = '+IntToStr(Cnt));
end;
 
</syntaxhighlight>
{{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
Count = 301
 
</pre>
 
 
 
=={{header|F_Sharp|F#}}==
Line 1,025 ⟶ 1,290:
301 such numbers found.
</pre>
The above solution uses lazy computation and tail recursion. Here's an eager solution, but it allocates a lot of intermediate sequences.
<syntaxhighlight lang="factor">USING: formatting kernel math ranges sequences ;
IN: rosetta-code.needs-a..f?
 
: quads ( n -- seq )
[ dup 0 > ] [ 16 /mod ] produce nip ;
 
: needs-a..f? ( n -- ? )
quads [ 9 > ] any? ;
 
500 [1..b] [ needs-a..f? ] filter [ "%d " printf ] each</syntaxhighlight>
{{out}}Same numbers as above, but on a single line.
 
=={{header|FOCAL}}==
Line 1,129 ⟶ 1,406:
 
301 such numbers found.
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function needs_af( byval n as uinteger ) as boolean
while n>0
if n mod 16 > 9 then return true
n\=16
wend
return false
end function
 
for i as uinteger = 1 to 500
if needs_af(i) then print i;" ";
next i
</syntaxhighlight>
{{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
</pre>
 
Line 1,279 ⟶ 1,539:
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
500</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">needsAtoF=. (9 +./@:< 16&#.inv)"0
 
_16 echo\ I. needsAtoF i. 501</syntaxhighlight>
{{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
</pre>
 
=={{header|JavaScript}}==
Line 1,527 ⟶ 1,814:
def hex:
def stream:
recurse(if . >= 016 then ./16|floor else empty end) | . % 16 ;
[stream] | reverse
if . == 0 then "0"
else [stream] | reverse | .[1:]
| map(if . < 10 then 48 + . else . + 87 end) | implode
end;
Line 1,706 ⟶ 1,992:
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 </pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec has_xdigit n =
n land 15 > 9 || n > 15 && has_xdigit (n lsr 4)
 
let () =
Seq.(ints 1 |> take 500 |> filter has_xdigit |> map string_of_int)
|> List.of_seq |> String.concat " " |> print_endline</syntaxhighlight>
{{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</pre>
 
=={{header|Perl}}==
Line 2,242 ⟶ 2,538:
done...
</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ 1 CF
'''WHILE''' DUP '''REPEAT'''
16 MOD LAST / IP SWAP
'''IF''' 9 > '''THEN''' 1 SF '''END'''
'''END''' DROP 1 FS?
≫ ''''A2F?'''' STO
 
≪ { } 1 500 '''FOR''' n '''IF''' n '''A2F? THEN''' n + '''END NEXT''' 440 .2 BEEP ≫ EVAL
{{out}}
<pre>
1: { 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 }
</pre>
Result coming after 505 seconds on a basic HP-28S, I have added a <code>BEEP</code> instruction at the end of the code. Thus, the program can be used to deliver at the same time 301 numbers needing A to F in base 16 and perfectly cooked ''al dente'' pasta.
===Optimized version===
Faster by 10%:
≪ 1 CF
'''WHILE REPEAT'''
'''IF''' LAST DUP 16 MOD 9 > '''THEN''' NOT 1 SF '''ELSE''' 16 / IP '''END'''
'''END''' 1 FS?
≫ ''''A2F?'''' STO
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">puts (0..500).select{|n| n.digits(16).any?{|d| d >= 10} }.join(" ")</syntaxhighlight>
Line 2,307 ⟶ 2,626:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var nondecimal = "abcdef"
Line 2,391 ⟶ 2,710:
301 such numbers found.
</pre>
 
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">sub needs_af (n)
while n > 0
if mod(n, 16) > 9 then return true : fi
n = int(n / 16)
wend
return false
end sub
 
for i = 1 to 500
if needs_af(i) then print i, " ", : fi
next i
end</syntaxhighlight>
559

edits