Skip to main content
added 16 characters in body
Source Link
reffu
  • 2k
  • 10
  • 10

Forth (gforth), 103103 99 bytes

: f 9 swap 0 do begin 1+ dup dup 0 swapover begin 10 /mod >r swap 10 * + r> ?dup 0= until = 0= until loop ;

Try it online!Try it online!

: f                \ start a new word definition
  9                \ start at 9, since all positive ints < 10 are palindromic
  swap 0           \ set up loop parameters from 0 to n-1
  do               \ start a counted loop       
    begin          \ start an indefinite loop
      1+ dup dup      \ increment counter and place twoa copiescopy on the stack
      ( Reverse )
      0 swapover       \ add 0 to the stack (as a buffer) and move it belowcopy the top counter copyabove it
      begin        \ start another indefinite loop
        10 /mod    \ get the quotient and remainder of dividing the number by 10
        >r         \ store the quotient on the return stack
        swap 10 *  \ multiply the current buffer by 10
        +          \ add the remainder to the buffer
        r>         \ grab the quotient from the return stack
        ?dup       \ duplicate if not equal to 0
        0=         \ check if equal to 0
      until        \ end inner indefinite loop if quotient is 0
      ( End Reverse )
      = 0=         \ check if counter =/= reverse-counter            
    until          \ end the outer indefinite loop if counter =/= reverse-counter
  loop             \ end the counted loop
;                  \ end the word definition 

Forth (gforth), 103 bytes

: f 9 swap 0 do begin 1+ dup dup 0 swap begin 10 /mod >r swap 10 * + r> ?dup 0= until = 0= until loop ;

Try it online!

: f                \ start a new word definition
  9                \ start at 9, since all ints < 10 are palindromic
  swap 0           \ set up loop parameters from 0 to n-1
  do               \ start a counted loop       
    begin          \ start an indefinite loop
      1+ dup dup   \ increment counter and place two copies on the stack
      ( Reverse )
      0 swap       \ add 0 to the stack (as a buffer) and move it below the top counter copy
      begin        \ start another indefinite loop
        10 /mod    \ get the quotient and remainder of dividing the number by 10
        >r         \ store the quotient on the return stack
        swap 10 *  \ multiply the current buffer by 10
        +          \ add the remainder to the buffer
        r>         \ grab the quotient from the return stack
        ?dup       \ duplicate if not equal to 0
        0=         \ check if equal to 0
      until        \ end inner indefinite loop if quotient is 0
      ( End Reverse )
      = 0=         \ check if counter =/= reverse-counter            
    until          \ end the outer indefinite loop if counter =/= reverse-counter
  loop             \ end the counted loop
;                  \ end the word definition 

Forth (gforth), 103 99 bytes

: f 9 swap 0 do begin 1+ dup 0 over begin 10 /mod >r swap 10 * + r> ?dup 0= until = 0= until loop ;

Try it online!

: f                \ start a new word definition
  9                \ start at 9, since all positive ints < 10 are palindromic
  swap 0           \ set up loop parameters from 0 to n-1
  do               \ start a counted loop       
    begin          \ start an indefinite loop
      1+ dup       \ increment counter and place a copy on the stack
      ( Reverse )
      0 over       \ add 0 to the stack (as a buffer) and copy the top counter above it
      begin        \ start another indefinite loop
        10 /mod    \ get the quotient and remainder of dividing the number by 10
        >r         \ store the quotient on the return stack
        swap 10 *  \ multiply the current buffer by 10
        +          \ add the remainder to the buffer
        r>         \ grab the quotient from the return stack
        ?dup       \ duplicate if not equal to 0
        0=         \ check if equal to 0
      until        \ end inner indefinite loop if quotient is 0
      ( End Reverse )
      = 0=         \ check if counter =/= reverse-counter            
    until          \ end the outer indefinite loop if counter =/= reverse-counter
  loop             \ end the counted loop
;                  \ end the word definition 
Source Link
reffu
  • 2k
  • 10
  • 10

Forth (gforth), 103 bytes

: f 9 swap 0 do begin 1+ dup dup 0 swap begin 10 /mod >r swap 10 * + r> ?dup 0= until = 0= until loop ;

Try it online!

###Explanation Loop n times, each iteration finds the next non-palindromic number by incrementing a counter by 1 until the number doesn't equal itself reversed

###Ungolfed Code Normally I wouldn't "ungolf" the code, but since this code is somewhat messy I figured it would help

: reverse ( s -- s )
    0 swap 
    begin 
        10 /mod
        >r swap
        10 * +
        r> ?dup 0=
    until 
; 

: f ( s -- s )
    9 swap 0
    0
    do
        begin
            1+ dup dup
            reverse =
        0= until
    loop
;

###Code Explanation

: f                \ start a new word definition
  9                \ start at 9, since all ints < 10 are palindromic
  swap 0           \ set up loop parameters from 0 to n-1
  do               \ start a counted loop       
    begin          \ start an indefinite loop
      1+ dup dup   \ increment counter and place two copies on the stack
      ( Reverse )
      0 swap       \ add 0 to the stack (as a buffer) and move it below the top counter copy
      begin        \ start another indefinite loop
        10 /mod    \ get the quotient and remainder of dividing the number by 10
        >r         \ store the quotient on the return stack
        swap 10 *  \ multiply the current buffer by 10
        +          \ add the remainder to the buffer
        r>         \ grab the quotient from the return stack
        ?dup       \ duplicate if not equal to 0
        0=         \ check if equal to 0
      until        \ end inner indefinite loop if quotient is 0
      ( End Reverse )
      = 0=         \ check if counter =/= reverse-counter            
    until          \ end the outer indefinite loop if counter =/= reverse-counter
  loop             \ end the counted loop
;                  \ end the word definition