I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Category:Factor-numspec

From Rosetta Code
Library
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:

USING: math numspec prettyprint ;

Examples[edit]

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:

USING: lists numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: a 3_3 ;
a list>array .
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.

USING: lists numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: b 3 33 3_3 ;
b list>array .
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.

USING: lists lists.lazy numspec prettyprint ;
DIGIT: _ 0123456789
NUMSPEC: c 3 33 3_3 ... ;
c [ 3030 < ] lwhile
list>array .
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:

USING: lists lists.lazy numspec prettyprint ;
DIGIT: P 2357
NUMSPEC: d P PP ... ;
20 d ltake list>array .
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 9s:

USING: lists lists.lazy numspec prettyprint ;
DIGIT: V 25
NUMSPEC: e 9 99 9V9 ... ;
30 e ltake list>array .
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.