test for repdigit - so called "Schnapszahl in German"

109 views
Skip to first unread message

ku...@physik.rwth-aachen.de

unread,
Jul 2, 2022, 3:40:42 AMJul 2
to
Probably because a drunken person may see double digits a repdigit number is called "Schnapszahl". Anyway, don't want to bother you with local peculiarities, instead give a weekend riddle: How do I test (most elegantly as always is a demand in FORTH :) whether a number consists of equal digits before being printed in the current base?

Idea behind is in what base the decimal numbers 42 and 80 are repdigit numbers?

The FORTH words I have so far are:

: b base ! . decimal ; ok

: t 81 2 do cr i ." base:" . ." 80 ->" 80 i b ." 42-> " 42 i b loop ;

Would be fine if FORTH could sort out the hits rather than the human eye.

Zbig

unread,
Jul 2, 2022, 8:33:57 AMJul 2
to
: findBase 81 2 do i base ! dup base @ /mod = if ." Got it! Base: " base @ decimal . leave then loop drop decimal ;

Zbig

unread,
Jul 2, 2022, 8:54:44 AMJul 2
to
> : findBase 81 2 do i base ! dup base @ /mod = if ." Got it! Base: " base @ decimal . leave then loop drop decimal ;

OK, a little refined to limit memory access:
: findBase2 81 2 do base dup i swap ! @ over swap /mod = if ." Got it! Base: " i decimal . leave then loop drop ;
(and that closing "decimal" not needed as long as you'll search the numbers not larger than 80)

Alexander Wegel

unread,
Jul 2, 2022, 1:14:52 PMJul 2
to
The following detects Schnapszahlen having two places only (and gives
false results outside that range) for the sake of simplicity:

: ten base @ ;
: snaps? ten 1+ mod 0= ;

The thing to take home: a schnapszahl is a multiple of 11 in the current
number base.

cheers

Alexander Wegel

unread,
Jul 2, 2022, 1:18:21 PMJul 2
to
Alexander Wegel <awe...@arcor.de> wrote:

> The thing to take home: a schnapszahl is a multiple of 11 in the current
> number base.

Anyway, it's just the same principle as in zbigs code 8-)

ku...@physik.rwth-aachen.de

unread,
Jul 3, 2022, 3:26:16 AMJul 3
to
Alexander, thanks for the "multiple of 11" remark, but doesn't help in the problem solution.
One could extend the rule to 111 for 3 digit results and so on.

@Zbig: the problem to solve is: name all bases in which the given number (80,42) is a repdigit number.
For the 80, the result should be:

80
base number
3 2222
9 22
19 44
39 22
79 11

Your algorithm finds the 9 (only)


42
base number
4 222
13 33
20 22
41 11


Zbig

unread,
Jul 3, 2022, 3:47:21 AMJul 3
to
> @Zbig: the problem to solve is: name all bases in which the given number (80,42) is a repdigit number.

No, that ("all bases") wasn't present in the conditions.

> For the 80, the result should be:
>
> 80
> base number
> 3 2222
> 9 22
> 19 44
> 39 22
> 79 11
>
> Your algorithm finds the 9 (only)

You can simply remove "leave" word -- or (even better) replace it with "cr".
As for the schnapsen with more than two digits: it's doable, but I think it
would need some recursive algorithm.

Gerry Jackson

unread,
Jul 3, 2022, 3:49:10 AMJul 3
to
On 03/07/2022 08:26, ku...@physik.rwth-aachen.de wrote:
> Alexander Wegel schrieb am Samstag, 2. Juli 2022 um 19:18:21 UTC+2:
>> Alexander Wegel <awe...@arcor.de> wrote:
>>
>>> The thing to take home: a schnapszahl is a multiple of 11 in the current
>>> number base.
>> Anyway, it's just the same principle as in zbigs code 8-)
>
> Alexander, thanks for the "multiple of 11" remark, but doesn't help in the problem solution.
> One could extend the rule to 111 for 3 digit results and so on.
>
> @Zbig: the problem to solve is: name all bases in which the given number (80,42) is a repdigit number.
> For the 80, the result should be:
>
> 80
> base number
> 3 2222
> 9 22

22 in base 9 is decimal 20

> 19 44
> 39 22
> 79 11
>
> Your algorithm finds the 9 (only)
>
>
> 42
> base number
> 4 222
> 13 33
> 20 22
> 41 11: divisor ( #digits -- n-ones ) 0 tuck ?do base @ * 1+ loop ;

\ Extending Alexander Wegel's idea to 11...1, in bases 2 to 36 ANS max
\ base

: #digits ( ud -- #digs ) <# #s #> nip ;

: repdigit? ( ud -- f )
2dup #digits dup 2 < if drop nip exit then
divisor sm/rem drop 0=
;

: report ( n f -- )
0= if drop exit then
base @ >r decimal cr ." Base: " r@ 2 .r
." , Number: #" dup 0 .r
r> base ! ." , Repdigit: " .
;

: repdigit ( n -- )
dup 0 ( -- n ud )
37 2
do
i base ! 2>r
2r@ repdigit? ( -- n f )
over swap report 2r> ( -- n ud )
loop 2drop drop decimal
;

#42 repdigit cr
Base: 4, Number: #42, Repdigit: 222
Base: 13, Number: #42, Repdigit: 33
Base: 20, Number: #42, Repdigit: 22 ok

#80 repdigit
Base: 3, Number: #80, Repdigit: 2222
Base: 9, Number: #80, Repdigit: 88
Base: 15, Number: #80, Repdigit: 55
Base: 19, Number: #80, Repdigit: 44 ok


--
Gerry

ku...@physik.rwth-aachen.de

unread,
Jul 3, 2022, 4:14:34 AMJul 3
to
Gerry Jackson schrieb am Sonntag, 3. Juli 2022 um 09:49:10 UTC+2:
> On 03/07/2022 08:26, ku...@physik.rwth-aachen.de wrote:
> > Alexander Wegel schrieb am Samstag, 2. Juli 2022 um 19:18:21 UTC+2:
> >> Alexander Wegel <awe...@arcor.de> wrote:
> >>
> >>> The thing to take home: a schnapszahl is a multiple of 11 in the current
> >>> number base.
> >> Anyway, it's just the same principle as in zbigs code 8-)
> >
> > Alexander, thanks for the "multiple of 11" remark, but doesn't help in the problem solution.
> > One could extend the rule to 111 for 3 digit results and so on.
> >
> > @Zbig: the problem to solve is: name all bases in which the given number (80,42) is a repdigit number.
> > For the 80, the result should be:
> >
> > 80
> > base number
> > 3 2222
> > 9 22
> 22 in base 9 is decimal 20


Sorry, this meant to be 9 88. One more reason to let FORTH do the job and then copy/paste :)



ku...@physik.rwth-aachen.de

unread,
Jul 3, 2022, 4:27:13 AMJul 3
to
ku...@physik.rwth-aachen.de schrieb am Sonntag, 3. Juli 2022 um 10:14:34 UTC+2:
> Gerry Jackson schrieb am Sonntag, 3. Juli 2022 um 09:49:10 UTC+2:

Nice solution, Gerry. Only solutions
80:
base number
39 22
79 11
are missing.

Zbig

unread,
Jul 3, 2022, 4:28:02 AMJul 3
to
> You can simply remove "leave" word -- or (even better) replace it with "cr".
> As for the schnapsen with more than two digits: it's doable, but I think it
> would need some recursive algorithm.

Uh, I forgot: in such case we also need to insert "decimal" at the end back.
So to sum up it'll look like this:

: findBase 81 2 do base dup i swap ! @ over swap /mod = if ." Got it! Base: " i decimal . cr then loop drop decimal ;
80 findbase2 Got it! Base: 9
Got it! Base: 15
Got it! Base: 19
Got it! Base: 39
Got it! Base: 79
It found even more for 80, than you expected. :)

ku...@physik.rwth-aachen.de

unread,
Jul 3, 2022, 4:31:10 AMJul 3
to
ku...@physik.rwth-aachen.de schrieb am Sonntag, 3. Juli 2022 um 10:14:34 UTC+2:
> Gerry Jackson schrieb am Sonntag, 3. Juli 2022 um 09:49:10 UTC+2:
But ok, extending

: repdigit ( n -- )
dup 0 ( -- n ud )
80 2
do
i base ! 2>r compiled

ku...@physik.rwth-aachen.de

unread,
Jul 3, 2022, 4:35:27 AMJul 3
to
Zbig schrieb am Sonntag, 3. Juli 2022 um 10:28:02 UTC+2:
...

ku...@physik.rwth-aachen.de

unread,
Jul 3, 2022, 4:37:44 AMJul 3
to
Zbig schrieb am Sonntag, 3. Juli 2022 um 10:28:02 UTC+2:
...
> : findBase 81 2 do base dup i swap ! @ over swap /mod = if ." Got it! Base: " i decimal . cr then loop drop decimal ;
> 80 findbase2 Got it! Base: 9
> Got it! Base: 15
> Got it! Base: 19
> Got it! Base: 39
> Got it! Base: 79
> It found even more for 80, than you expected. :)

My first approach liested them all, but as said, the human eye is the weak link in the chain of recognition.

Gerry Jackson

unread,
Jul 3, 2022, 5:44:37 AMJul 3
to
THe Forth I use only goes up to base 36

--
Gerry

Alexander Wegel

unread,
Jul 3, 2022, 7:06:59 AMJul 3
to
ku...@physik.rwth-aachen.de <ku...@physik.rwth-aachen.de> wrote:

> Alexander Wegel schrieb am Samstag, 2. Juli 2022 um 19:18:21 UTC+2:
> > Alexander Wegel <awe...@arcor.de> wrote:
> >
> > > The thing to take home: a schnapszahl is a multiple of 11 in the current
> > > number base.
> > Anyway, it's just the same principle as in zbigs code 8-)
>
> Alexander, thanks for the "multiple of 11" remark, but doesn't help in the
> problem solution.
> One could extend the rule to 111 for 3 digit results and so on.

Yes, i was confused by your mentioning of "double digits", which are
apparently not part of the problem posed.

ku...@physik.rwth-aachen.de

unread,
Jul 3, 2022, 7:09:36 AMJul 3
to
@Zbig:
Your
: findBase 81 2 do base dup i swap ! @ over swap /mod = if ." Got it! Base: " i decimal . cr then loop drop decimal ;

doesn't find Base 3 2222 as the result.

BTW, my problem outline was :
"Idea behind is in what base the decimal numbers 42 and 80 are repdigit numbers? "

I think, this implies "all bases".

Marcel Hendrix

unread,
Jul 3, 2022, 7:10:49 AMJul 3
to
I had no time for a simple solution.

: same? ( c-addr u -- f )
DUP 1 = IF 2DROP FALSE EXIT ENDIF
over C@ >S 1 /STRING
0 ?DO C@+ S
<> IF DROP -S FALSE UNLOOP EXIT
ENDIF
LOOP
DROP -S TRUE ;

: ?. ( u -- ) dup (.) same? IF . ELSE DROP ENDIF ;

: .solutions ( n1 -- )
>S BASE @
#81 2 DO I BASE !
S (.) same? IF CR BASE @ DEC. S ?. ENDIF
LOOP
BASE ! -S ;

FORTH> 42 .solutions
4 222
13 33
20 22
41 11 ok
FORTH> 80 .solutions
3 2222
9 88
15 55
19 44
39 22
79 11 ok
FORTH>

-marcel

Marcel Hendrix

unread,
Jul 3, 2022, 7:22:31 AMJul 3
to
On Sunday, July 3, 2022 at 1:09:36 PM UTC+2, ku...@physik.rwth-aachen.de wrote:
[..]
> BTW, my problem outline was :
> "Idea behind is in what base the decimal numbers 42 and 80 are repdigit numbers? "
> I think, this implies "all bases".

What's so special about 42 and 80?

FORTH> : test #81 1 do i .solutions loop ; ok
FORTH> test
2 11
3 11
4 11
5 11
2 111
6 11
3 22
7 11
8 11
4 22
9 11
10 11
5 22
11 11
3 111
12 11
6 22
13 11
2 1111
4 33
14 11
7 22
15 11
16 11
5 33
8 22
17 11
18 11
9 22
19 11
4 111
6 33
20 11
10 22
21 11
22 11
5 44
7 33
11 22
23 11
24 11
3 222
12 22
25 11
8 33
26 11
6 44
13 22
27 11
28 11
9 33
14 22
29 11
2 11111
5 111
30 11
7 44
15 22
31 11
10 33
32 11
16 22
33 11
6 55
34 11
8 44
11 33
17 22
35 11
36 11
18 22
37 11
12 33
38 11
3 1111
7 55
9 44
19 22
39 11
40 11
4 222
13 33
20 22
41 11
6 111
42 11
10 44
21 22
43 11
8 55
14 33
44 11
22 22
45 11
46 11
7 66
11 44
15 33
23 22
47 11
48 11
9 55
24 22
49 11
16 33
50 11
12 44
25 22
51 11
52 11
8 66
17 33
26 22
53 11
10 55
54 11
13 44
27 22
55 11
7 111
18 33
56 11
28 22
57 11
58 11
9 66
11 55
14 44
19 33
29 22
59 11
60 11
5 222
30 22
61 11
2 111111
4 333
8 77
20 33
62 11
15 44
31 22
63 11
12 55
64 11
10 66
21 33
32 22
65 11
66 11
16 44
33 22
67 11
22 33
68 11
9 77
13 55
34 22
69 11
70 11
11 66
17 44
23 33
35 22
71 11
8 111
72 11
36 22
73 11
14 55
24 33
74 11
18 44
37 22
75 11
10 77
76 11
12 66
25 33
38 22
77 11
78 11
3 2222
9 88
15 55
19 44
39 22
79 11 ok
FORTH>

-marcel

Marcel Hendrix

unread,
Jul 3, 2022, 7:29:56 AMJul 3
to
On Sunday, July 3, 2022 at 1:22:31 PM UTC+2, Marcel Hendrix wrote:
> On Sunday, July 3, 2022 at 1:09:36 PM UTC+2, ku...@physik.rwth-aachen.de wrote:
> [..]
> > BTW, my problem outline was :
> > "Idea behind is in what base the decimal numbers 42 and 80 are repdigit numbers? "
> > I think, this implies "all bases".
> What's so special about 42 and 80?
>
> FORTH> : test #81 1 do i .solutions loop ; ok
> FORTH> test
[..]

60 is special in that it has the most schnappses.
testing with ** 60 **
9 66
11 55
14 44
19 33
29 22
59 11

-marcel

Zbig

unread,
Jul 3, 2022, 10:50:50 AMJul 3
to
No, it doesn't; it says: "find a base meeting a condition ... " (not "find all possible bases
in the range ... ").
So you don't want to follow the path and complete it by yourself?
OK, have a solution then; it's somewhat crude, but we've got Sunday in Poland today, you know:

variable wrong
0 wrong !
: num2str abs 0 <# #s swap #> ;
: checkIt over c@ -rot 0 do dup i + c@ >r over r> = 0= if 1 wrong ! leave then loop 2drop ;
: findBases cr 81 min dup 2 do base i swap ! dup num2str checkIt wrong @ 0= if decimal ." Schapps at the base " i . cr then 0 wrong ! loop drop decimal ;

The test:

80 findBases
Schapps at the base 3
Schapps at the base 9
Schapps at the base 15
Schapps at the base 19
Schapps at the base 39
Schapps at the base 79

42 findBases
Schapps at the base 4
Schapps at the base 13
Schapps at the base 20
Schapps at the base 41

dxforth

unread,
Jul 3, 2022, 11:07:03 AMJul 3
to
On 3/07/2022 21:10, Marcel Hendrix wrote:
>
> I had no time for a simple solution.
>
> : same? ( c-addr u -- f )
> DUP 1 = IF 2DROP FALSE EXIT ENDIF
> over C@ >S 1 /STRING
> 0 ?DO C@+ S
> <> IF DROP -S FALSE UNLOOP EXIT
> ENDIF
> LOOP
> DROP -S TRUE ;
>
> : .solutions ( n1 -- )
> >S BASE @
> #81 2 DO I BASE !
> S (.) same? IF CR BASE @ DEC. S ?. ENDIF
> LOOP
> BASE ! -S ;

: same? ( c-addr u -- f )
DUP 1 > IF
OVER C@ >R 1 /STRING
R> SKIP 0= NIP EXIT
THEN 2DROP FALSE ;

: .solutions ( n1 -- )
BASE @ SWAP
81 2 DO I BASE !
DUP (.) same? IF CR I DEC. DUP ?. THEN
LOOP DROP BASE ! ;


Marcel Hendrix

unread,
Jul 3, 2022, 12:24:27 PMJul 3
to
On Sunday, July 3, 2022 at 5:07:03 PM UTC+2, dxforth wrote:
[..]
> : same? ( c-addr u -- f )
> DUP 1 > IF
> OVER C@ >R 1 /STRING
> R> SKIP 0= NIP EXIT
> THEN 2DROP FALSE ;

What if there are n (>2) digits and less than n are different?
Or are you prepared to spend a day proving that that is
not possible :--?

-marcel

dxforth

unread,
Jul 3, 2022, 10:32:54 PMJul 3
to
It demonstrates what's possible without much effort. Are you
saying it produces results different from yours?

Marcel Hendrix

unread,
Jul 4, 2022, 1:16:53 AMJul 4
to
I'm sorry! I see now you have inverted the exit test and then
SKIP is indeed possible. I didn't think enough about it and
used the ugly LOOP with all its administrative problems.

-marcel

ku...@physik.rwth-aachen.de

unread,
Jul 4, 2022, 2:39:44 AMJul 4
to
Thanks to all for the discussion and interesting solutions. In our local newspaper there is a math riddle column
coming up with some brainteaser every weekend. The 42 is special since it is mentioned e.g. here:
https://www.scientificamerican.com/article/for-math-fans-a-hitchhikers-guide-to-the-number-42/
I have no idea about the 80. all which comes to mind is, that the column width of punched cards was 80 :)

--
Christoph

none albert

unread,
Jul 4, 2022, 3:31:17 AMJul 4
to
In article <t9rhlk$2uh87$1...@dont-email.me>,
A better algorithm is this for every BASE and number:

1. BASE > number , trivially true
2. n' = base + 1
3. temp= n' * base + 1
If temp > number, goto 4
set n' to temp, and goto 2
4. if n' divides number, number is repdigit
terminate
>
>
>--
>Gerry

Groetjes Albert
--
"in our communism country Viet Nam, people are forced to be
alive and in the western country like US, people are free to
die from Covid 19 lol" duc ha
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
Reply all
Reply to author
Forward
0 new messages