Category:Factor-numspec: Difference between revisions
(Add Factor-numspec library/vocabulary) |
m (Chunes moved page Factor-numspec to Category:Factor-numspec: to make libheader template work correctly) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 2:
'''Factor-numspec''' is a vocabulary that aims to make tasks involving the restriction of digits within numbers easy. This vocabulary was made because there are a fair number of tasks on Rosetta Code involving this operation and more being added all the time.
To use it yourself, copy the source code (in the [
Finally, in your program, make sure to add <code>numspec</code> to your <code>USING:</code> like so:
Line 9:
=== Examples ===
It's important to remember that a number specification
Get all three-digit numbers beginning and ending with <code>3</code>:
<lang factor>USING: lists numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: a 3_3 ;
a list>array .</lang>
{{out}}
<pre>
Line 22 ⟶ 23:
Literals can be added where necessary. Let's make special cases for one- and two-digit numbers.
<lang factor>USING: lists numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: b 3 33 3_3 ;
b list>array .</lang>
{{out}}
<pre>
Line 31 ⟶ 33:
But what if you don't want to make a case for every number of digits? Got you covered! <code>...</code> rudimentarily infers the pattern.
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: c 3 33 3_3 ... ;
c [ 3030 < ] lwhile
list>array .</lang>
{{out}}
Line 40 ⟶ 43:
Since this is an infinite list, we need to remember to limit it in some fashion (like with <code>lwhile</code>).
Numbers with only prime digits:
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: P 2357
20 d ltake list>array .</lang>
{{out}}
<pre>
{ 2 3 5 7 22 23 25 27 32 33 35 37 52 53 55 57 72 73 75 77 }
</pre>
<code>2</code> and <code>5</code> surrounded by <code>9</code>s:
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: V 25
NUMSPEC: e 9 99 9V9 ... ;
30
{{out}}
<pre>
|
Latest revision as of 22:02, 5 August 2021
This is an example of a library. You may see a list of other libraries used on Rosetta Code at Category:Solutions by Library.
Factor-numspec is a vocabulary that aims to make tasks involving the restriction of digits within numbers easy. This vocabulary was made because there are a fair number of tasks on Rosetta Code involving this operation and more being added all the time.
To use it yourself, copy the source code (in the talk page) to a text file called numspec.factor
and then save it in a sub-directory called numspec
inside your work
directory. work
is in the same directory as the Factor executable.
Finally, in your program, make sure to add numspec
to your USING:
like so:
<lang factor>USING: math numspec prettyprint ;</lang>
Examples
It's important to remember that a number specification defines a named lazy list constant. Whether the list ends or not depends on whether you end the specification with ...
.
Get all three-digit numbers beginning and ending with 3
:
<lang factor>USING: lists numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: a 3_3 ;
a list>array .</lang>
- Output:
{ 303 313 323 333 343 353 363 373 383 393 }
Literals can be added where necessary. Let's make special cases for one- and two-digit numbers. <lang factor>USING: lists numspec prettyprint ; DIGIT: _ 0123456789 NUMSPEC: b 3 33 3_3 ; b list>array .</lang>
- Output:
{ 3 33 303 313 323 333 343 353 363 373 383 393 }
But what if you don't want to make a case for every number of digits? Got you covered! ...
rudimentarily infers the pattern.
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: c 3 33 3_3 ... ;
c [ 3030 < ] lwhile
list>array .</lang>
- Output:
{ 3 33 303 313 323 333 343 353 363 373 383 393 3003 3013 3023 }
Since this is an infinite list, we need to remember to limit it in some fashion (like with lwhile
).
Numbers with only prime digits: <lang factor>USING: lists lists.lazy numspec prettyprint ; DIGIT: P 2357 NUMSPEC: d P PP ... ; 20 d ltake list>array .</lang>
- Output:
{ 2 3 5 7 22 23 25 27 32 33 35 37 52 53 55 57 72 73 75 77 }
2
and 5
surrounded by 9
s:
<lang factor>USING: lists lists.lazy numspec prettyprint ;
DIGIT: V 25
NUMSPEC: e 9 99 9V9 ... ;
30 e ltake list>array .</lang>
- Output:
{ 9 99 929 959 9229 9259 9529 9559 92229 92259 92529 92559 95229 95259 95529 95559 922229 922259 922529 922559 925229 925259 925529 925559 952229 952259 952529 952559 955229 955259 }
Pages in category "Factor-numspec"
The following 2 pages are in this category, out of 2 total.