: findBase 81 2 do i base ! dup base @ /mod = if ." Got it! Base: " base @ decimal . leave then loop drop decimal ;
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.
The thing to take home: a schnapszahl is a multiple of 11 in the current number base.
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-)
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:
8022 in base 9 is decimal 20
base number
3 2222
9 22
@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)
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
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 ;
Gerry Jackson schrieb am Sonntag, 3. Juli 2022 um 09:49:10 UTC+2:But ok, extending
Gerry Jackson schrieb am Sonntag, 3. Juli 2022 um 09:49:10 UTC+2:
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.
: 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. :)
: 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. :)
kuku@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.
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 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 ;My first approach liested them all, but as said, the human eye is the weak link in the chain of recognition.
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. :)
On Sunday, July 3, 2022 at 1:09:36 PM UTC+2, ku...@physik.rwth-aachen.de wrote:[..]
[..]
BTW, my problem outline was :What's so special about 42 and 80?
"Idea behind is in what base the decimal numbers 42 and 80 are repdigit numbers? "
I think, this implies "all bases".
FORTH> : test #81 1 do i .solutions loop ; ok
FORTH> test
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".
: findBase 81 2 do base dup i swap ! @ over swap /mod = if ." Got it! Base: " i decimal . cr then loop drop decimal ;No, it doesn't; it says: "find a base meeting a condition ... " (not "find all possible bases
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".
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
SKIP 0= NIP EXITTHEN 2DROP FALSE ;
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
SKIP 0= NIP EXITTHEN 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 :--?
On 4/07/2022 02:24, Marcel Hendrix wrote:
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
SKIP 0= NIP EXITTHEN 2DROP FALSE ;
What if there are n (>2) digits and less than n are different?It demonstrates what's possible without much effort. Are you
Or are you prepared to spend a day proving that that is
not possible :--?
saying it produces results different from yours?
On 03/07/2022 08:26, kuku@physik.rwth-aachen.de wrote:
Alexander Wegel schrieb am Samstag, 2. Juli 2022 um 19:18:21 UTC+2:the problem solution.
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
One could extend the rule to 111 for 3 digit results and so on.number (80,42) is a repdigit number.
@Zbig: the problem to solve is: name all bases in which the given
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
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 38:58:30 |
Calls: | 6,708 |
Calls today: | 1 |
Files: | 12,241 |
Messages: | 5,353,638 |