PARI/GP Program to Make a List of Swedish Numbers with Digits in Alphabetical Order


I was looking at The On-Line Encyclopedia of Integer Sequences when I ran across sequence A247809, which is described as “numbers in decimal representation with distinct digits, such that in Norwegian and Swedish their digits are in alphabetic order.”

July 18, 2019: I think sequence A247809 is wrong.

The “wrong” alphabetical order has: 8=åtta, 1=en/ett, 5=fem, 4=fyra, 0=noll, 6=sex, 7=sju, 9=tio, 2=två, 3=tre or 8, 1, 5, 4, 0, 6, 7, 9, 2, 3.

The “wrong” sequence is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 19, 23, 40, 42, 43, 46, 47, 49, 50, 52, 53, 54, 56, 57, 59, 62, 63, 67, 69, 72, 73, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 92, 93, 102, 103, 106, 107, 109, 123, 140, 142, 143, 146, 147, 149, 150, 152

The word tio (10) should probably be nio (9).1 2

The names are sorted using English sorting rules. In Swedish, the letter Å/å, like in åtta (8), comes after z. Swedish alphabetical order is a, …, z, å, ä, ö. Using Swedish sorting rules and nio for 9, the sequence for 0-9 would be 1, 5, 4, 9, 0, 6, 7, 3, 2, 8.3 4

Wrong Order 8 1 5 4 0 6 7 9 2 3
Correct Order 1 5 4 9 0 6 7 3 2 8

I doubt that the sequence has any value in real life, but it looked like a fun sequence. It would be great to use in an answer for questions like, “What is the next number in this sequence: 19, 22, 28, 32, 33, 38, 40, __?” (The answer is 42. The sequence is Swedish numbers where the digits are in alphabetical order.)

To run the program, you set up the parameters using these lines:

upper_limit = 100;
number_base = 2;
digits_order = digits_swedish;
language_name = “swedish”;

upper_limit is the upper limit for the list of numbers. For example, when set to 100, the list will contain number from 0 to 100 inclusive.

number_base should be fairly obvious; it’s the base-n for the numbers. For example, when set to two, the list will contain binary numbers.

digits_order is where you set the language you want to use. See below for extending the program to do other languages.

language_name is used for creating a file name for the output. For example, when upper_limit is set to 100, number_base is set to 2, and language_name is set to "swedish" it produces a file name like numbers-with-digits-in-alphabetical-order_swedish_0-100_base-2.txt.

digits_swedish = [1, 5, 4, 9, 0, 6, 7, 3, 2, 8];
\(\qquad\)
/*
* Set up parameters.
*/
\(\qquad\)
upper_limit = 100;
number_base = 2;
digits_order = digits_swedish;
language_name = “swedish”;
\(\qquad\)
output_file = Strprintf(“numbers-with-digits-in-alphabetical-order_%s_0-%d_base-%d.txt”, language_name, upper_limit, number_base);
\(\qquad\)
/*
* digits_alphabetical(x)
*/
\(\qquad\)
digits_alphabetical(x) = {
\(\qquad\)my (
\(\qquad\)\(\qquad\)digs = digits(x, number_base),
\(\qquad\)\(\qquad\)first = -1,
\(\qquad\)\(\qquad\)second = -1
\(\qquad\));
\(\qquad\)
\(\qquad\)for (y = 1, #digs – 1,
\(\qquad\)\(\qquad\)for (z = 1, #digits_order, if (digs[y] == digits_order[z], first = z));
\(\qquad\)\(\qquad\)for (z = 1, #digits_order, if (digs[y + 1] == digits_order[z], second = z));
\(\qquad\)\(\qquad\)if (first > second, return (-1))
\(\qquad\));
\(\qquad\)
\(\qquad\)return (x)
}
\(\qquad\)
/*
* Main routine.
*/
{
for (x = 0, upper_limit,
\(\qquad\)if (digits_alphabetical(x) != -1,
\(\qquad\)\(\qquad\)my (z = “”);
\(\qquad\)
\(\qquad\)\(\qquad\)\\ Print to the screen so we can see something happening.
\(\qquad\)\(\qquad\)print1(x, “, “);
\(\qquad\)
\(\qquad\)\(\qquad\)\\ Build the base-n number from the array.
\(\qquad\)\(\qquad\)base_num = digits(x, number_base);
\(\qquad\)
\(\qquad\)for (y = 1, #base_num, z = concat(z, base_num[y]));
\(\qquad\)
\(\qquad\)\(\qquad\)\\ If x == 0, adjust so we get something to print.
\(\qquad\)\(\qquad\)if (x == 0, z = 0);
\(\qquad\)
\(\qquad\)\(\qquad\)\\ If you want to print out the decimal number only, use this next line.
\(\qquad\)\(\qquad\)\\ write1(output_file, x, “, “);
\(\qquad\)
\(\qquad\)\(\qquad\)\\ If you want to print out the base-n number only, use this next line.
\(\qquad\)\(\qquad\)\\ write1(output_file, z, “, “)
\(\qquad\)
\(\qquad\)\(\qquad\)\\ If you want to print out the decimal number and the base-n number,
\(\qquad\)\(\qquad\)\\ use this next line.
\(\qquad\)\(\qquad\)write1(output_file, x, ” (“, z, “), “);
\(\qquad\))
)
}

If you want to extend the program to handle other languages, you can add lines like these.

digits_afrikaans = [8, 3, 1, 9, 0, 6, 7, 2, 4, 5];
digits_albanian = [2, 6, 4, 9, 1, 5, 7, 8, 3, 0];
digits_amharic = [2, 8, 3, 6, 7, 4, 5, 1, 9, 0];
digits_czech = [4, 9, 2, 1, 0, 8, 5, 7, 6, 3];
digits_danish = [1, 5, 4, 9, 0, 8, 6, 7, 2, 3];
digits_dutch = [8, 1, 3, 9, 0, 2, 4, 5, 6, 7];
digits_english = [8, 5, 4, 9, 1, 7, 6, 3, 2, 0];
digits_finnish = [8, 2, 3, 6, 4, 0, 7, 5, 9, 1];
digits_french = [5, 2, 8, 9, 4, 7, 6, 3, 1, 0];
digits_german = [8, 3, 1, 5, 9, 0, 6, 7, 4, 2];
digits_hausa = [7, 5, 2, 1, 4, 6, 0, 8, 9, 3];
digits_hungarian = [1, 3, 6, 7, 2, 9, 4, 0, 8, 5];
digits_icelandic = [8, 1, 5, 4, 9, 0, 6, 7, 2, 3];
digits_italian = [5, 2, 9, 8, 4, 6, 7, 3, 1, 0];
digits_klingon = [2, 8, 9, 6, 4, 0, 7, 5, 1, 3]; \\ Klingon!
digits_latin = [2, 9, 8, 4, 5, 6, 7, 3, 1, 0];
digits_norwegian = [8, 1, 5, 4, 0, 6, 7, 9, 2, 3]; \\ May be wrong.
digits_polish = [4, 2, 9, 1, 8, 5, 6, 7, 3, 0];
digits_portuguese = [5, 2, 9, 8, 4, 6, 7, 3, 1, 0];
digits_russian = [8, 2, 9, 0, 1, 5, 7, 3, 4, 6];
digits_slovak = [9, 2, 1, 0, 8, 5, 7, 6, 4, 3];
digits_somali = [4, 0, 1, 2, 6, 3, 9, 5, 8, 7];
digits_spanish = [0, 5, 4, 2, 9, 8, 6, 7, 3, 1];
digits_swedish = [1, 5, 4, 9, 0, 6, 7, 3, 2, 8];
digits_turkish = [6, 5, 1, 9, 4, 2, 8, 0, 3, 7];
digits_zulu = [0, 7, 9, 8, 6, 2, 5, 4, 1, 3];

Then change the following lines to your language of choice.

digits_order = digits_swedish;
language_name = “swedish”;

There may be more languages here: “Alphabetized Numbers from 0 to 9“. If you speak a language that is not in our collection, please leave a comment below.

Base 2 output [1, 0]:

0 (0), 1 (1), 2 (10), 3 (11), 4 (100), 6 (110), 7 (111), 8 (1000), 12 (1100), 14 (1110), 15 (1111), 16 (10000), 24 (11000), 28 (11100), 30 (11110), 31 (11111), 32 (100000), 48 (110000), 56 (111000), 60 (111100), 62 (111110), 63 (111111), 64 (1000000), 96 (1100000)

Base 3 output [1, 0, 2]:

0 (0), 1 (1), 2 (2), 3 (10), 4 (11), 5 (12), 8 (22), 9 (100), 11 (102), 12 (110), 13 (111), 14 (112), 17 (122), 26 (222), 27 (1000), 29 (1002), 35 (1022), 36 (1100), 38 (1102), 39 (1110), 40 (1111), 41 (1112), 44 (1122), 53 (1222), 80 (2222), 81 (10000), 83 (10002), 89 (10022)

Base 4 output [1, 0, 3, 2]:

0 (0), 1 (1), 2 (2), 3 (3), 4 (10), 5 (11), 6 (12), 7 (13), 10 (22), 14 (32), 15 (33), 16 (100), 18 (102), 19 (103), 20 (110), 21 (111), 22 (112), 23 (113), 26 (122), 30 (132), 31 (133), 42 (222), 58 (322), 62 (332), 63 (333), 64 (1000), 66 (1002), 67 (1003), 74 (1022), 78 (1032), 79 (1033), 80 (1100), 82 (1102), 83 (1103), 84 (1110), 85 (1111), 86 (1112), 87 (1113), 90 (1122), 94 (1132), 95 (1133)

Base 5 output [1, 4, 0, 3, 2]:

0 (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (10), 6 (11), 7 (12), 8 (13), 9 (14), 12 (22), 17 (32), 18 (33), 20 (40), 22 (42), 23 (43), 24 (44), 25 (100), 27 (102), 28 (103), 30 (110), 31 (111), 32 (112), 33 (113), 34 (114), 37 (122), 42 (132), 43 (133), 45 (140), 47 (142), 48 (143), 49 (144), 62 (222), 87 (322), 92 (332), 93 (333), 100 (400)

Base 6 output [1, 5, 4, 0, 3, 2]:

0 (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (5), 6 (10), 7 (11), 8 (12), 9 (13), 10 (14), 11 (15), 14 (22), 20 (32), 21 (33), 24 (40), 26 (42), 27 (43), 28 (44), 30 (50), 32 (52), 33 (53), 34 (54), 35 (55), 36 (100), 38 (102), 39 (103), 42 (110), 43 (111), 44 (112), 45 (113), 46 (114), 47 (115), 50 (122), 56 (132), 57 (133), 60 (140), 62 (142), 63 (143), 64 (144), 66 (150), 68 (152), 69 (153), 70 (154), 71 (155), 86 (222)

Base 7 output [1, 5, 4, 0, 6, 3, 2]:

0 (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (5), 6 (6), 7 (10), 8 (11), 9 (12), 10 (13), 11 (14), 12 (15), 13 (16), 16 (22), 23 (32), 24 (33), 28 (40), 30 (42), 31 (43), 32 (44), 34 (46), 35 (50), 37 (52), 38 (53), 39 (54), 40 (55), 41 (56), 44 (62), 45 (63), 48 (66), 49 (100), 51 (102), 52 (103), 55 (106), 56 (110), 57 (111), 58 (112), 59 (113), 60 (114), 61 (115), 62 (116), 65 (122), 72 (132), 73 (133), 77 (140), 79 (142), 80 (143), 81 (144), 83 (146), 84 (150), 86 (152), 87 (153), 88 (154), 89 (155), 90 (156), 93 (162), 94 (163), 97 (166)

Base 8 output [1, 5, 4, 0, 6, 7, 3, 2]:

0 (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (5), 6 (6), 7 (7), 8 (10), 9 (11), 10 (12), 11 (13), 12 (14), 13 (15), 14 (16), 15 (17), 18 (22), 26 (32), 27 (33), 32 (40), 34 (42), 35 (43), 36 (44), 38 (46), 39 (47), 40 (50), 42 (52), 43 (53), 44 (54), 45 (55), 46 (56), 47 (57), 50 (62), 51 (63), 54 (66), 55 (67), 58 (72), 59 (73), 63 (77), 64 (100), 66 (102), 67 (103), 70 (106), 71 (107), 72 (110), 73 (111), 74 (112), 75 (113), 76 (114), 77 (115), 78 (116), 79 (117), 82 (122), 90 (132), 91 (133), 96 (140), 98 (142), 99 (143), 100 (144)

Base 9 output [1, 5, 4, 0, 6, 7, 3, 2, 8]:

0 (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (5), 6 (6), 7 (7), 8 (8), 9 (10), 10 (11), 11 (12), 12 (13), 13 (14), 14 (15), 15 (16), 16 (17), 17 (18), 20 (22), 26 (28), 29 (32), 30 (33), 35 (38), 36 (40), 38 (42), 39 (43), 40 (44), 42 (46), 43 (47), 44 (48), 45 (50), 47 (52), 48 (53), 49 (54), 50 (55), 51 (56), 52 (57), 53 (58), 56 (62), 57 (63), 60 (66), 61 (67), 62 (68), 65 (72), 66 (73), 70 (77), 71 (78), 80 (88), 81 (100), 83 (102), 84 (103), 87 (106), 88 (107), 89 (108), 90 (110), 91 (111), 92 (112), 93 (113), 94 (114), 95 (115), 96 (116), 97 (117), 98 (118)

Base 10 output [1, 5, 4, 9, 0, 6, 7, 3, 2, 8]:

0 (0), 1 (1), 2 (2), 3 (3), 4 (4), 5 (5), 6 (6), 7 (7), 8 (8), 9 (9), 10 (10), 11 (11), 12 (12), 13 (13), 14 (14), 15 (15), 16 (16), 17 (17), 18 (18), 19 (19), 22 (22), 28 (28), 32 (32), 33 (33), 38 (38), 40 (40), 42 (42), 43 (43), 44 (44), 46 (46), 47 (47), 48 (48), 49 (49), 50 (50), 52 (52), 53 (53), 54 (54), 55 (55), 56 (56), 57 (57), 58 (58), 59 (59), 62 (62), 63 (63), 66 (66), 67 (67), 68 (68), 72 (72), 73 (73), 77 (77), 78 (78), 88 (88), 90 (90), 92 (92), 93 (93), 96 (96), 97 (97), 98 (98), 99 (99), 100 (100)

Have fun with it and if you find any interesting patterns, let us know in the comments.

Footnotes

  1. Wikipedia contributors, “List of numbers in various languages: Germanic languages“. Wikipedia.
  2. Tibor. “Swedish numbers 1-100“. Swedish Language Blog.
  3. Swedish speaker (not me).
  4. Wikipedia contributors. “Swedish alphabet“. Wikipedia.

Leave a Reply