Levenshtein distance & OEIS (Poliziotti)


16

Questo è il post di Cop. Il post del ladro è qui .


Il tuo compito è quello di prendere un input intero N ed emettere l' ennesima cifra nella sequenza OEIS A002942 .

La sequenza è composta dai numeri quadrati scritti al contrario:

1, 4, 9, 61, 52, 63, 94, 46, 18, 1, 121, 441, ...

Si noti che gli zeri iniziali vengono eliminati ( 100 diventa 1 , non 001 ). Concatenando questo in una stringa (o un numero lungo dà):

1496152639446181121441

Dovresti produrre l' ennesima cifra in questa stringa / numero. Puoi scegliere di prendere N come 0-indicizzato o 1-indicizzato (indica quale scegli).

Casi di test (1 indicizzato):

N = 1,      ==> 1
N = 5,      ==> 1
N = 17,     ==> 1   <- Important test case! It's not zero.
N = 20,     ==> 4
N = 78,     ==> 0
N = 100,    ==> 4

Il codice dovrebbe funzionare per numeri fino a N = 2 ^ 15 (a meno che la tua lingua non possa gestire numeri interi a 32 bit per impostazione predefinita, nel qual caso N può essere inferiore).


Cops:

Devi scrivere due funzioni / programmi, nella stessa lingua, che fanno la stessa cosa. Devi pubblicare una delle funzioni / programmi, così come la distanza di Levenshtein tra le due funzioni / programmi che hai scritto. La distanza di Levenshtein è misurata in caratteri (quindi l'aggiunta di un carattere a due byte darà un LD = 1).

Il codice non rivelato non può essere più lungo della soluzione originale (ma può avere le stesse dimensioni). I ladri tenteranno di scrivere un codice con la distanza esatta di Levenshtein che hai indicato (può essere diverso dal codice non rivelato, purché funzioni).

Il vincitore sarà l'invio senza crack che ha la distanza di Levenshtein più bassa.

Puoi controllare la distanza di Levenshtein qui!


Se il tuo invio non viene archiviato per 7 giorni, puoi rivelare il codice alternativo che hai scritto e contrassegnarlo come sicuro.



Sì, lo aggiungerò alle regole. :)
Stewie Griffin,

Parli di script, ma suppongo che questo sia il programma o la funzione predefiniti ?
Kevin Cruijssen,

Sì, impostazione predefinita tutto :)
Stewie Griffin,

Come stai gestendo i commenti e gli spazi bianchi, vedo alcune cose strane.
Magic Octopus Urn

1
"Il tuo codice dovrebbe funzionare per numeri fino a N = 2 ^ 15" -> ma uno dei casi di test è più grande di quello. Il caso 274164 è necessario?
Tom Carpenter,

Risposte:


6

Haskell , LD = 13, incrinato

((snd.span(<'1').reverse.show.(^2)=<<[1..])!!)

Provalo online!

Ho ricontrollato che gli zeri iniziali sono tagliati;)

Spiegazione:

                    [1..]     -- for each element in [1,2,3,4,5,...]
                 =<<          -- apply the following functions 
             (^2)             -- square [1,4,9,16,25,...]
           show.              -- convert to string ["1","4","9","16","25",...]
       reverse.               -- reverse ["1","4","9","61","52",...,"001",...]
   span(<'1').                -- split into leading zeros and remainder [("","1"),("","4"),...,("00","1"),...]
  snd.                        -- only keep remainder ["1","4","9","61","52",...,"1",...]
                              -- and concatenate the result "1496152..."
((                       )!!) -- index into the sequence


3

cQuents 0 , LD = 1, Cracked

":\r$*$

Provalo online!

Pensavo che non funzionasse per gli zeri iniziali, ma in realtà funziona: la funzione inversa in cQuents è codificata come int(reversed(str(n))).

Spiegazione

"         Concatenate sequence together, get nth term in the string instead of the sequence
 :        Mode: Sequence: given input n, output the nth term, 1-indexed
          Each term in the sequences equals:
  \r      reverse(
    $*$           the index * the index
                  or
    $$            the index * the index
                   ) (implicit)


@Laikoni sì, cQuents ha implicazioni come la moltiplicazione, un po 'come Mathematica, ma non hai bisogno degli spazi. Aggiungere una spiegazione
Stephen,

3

JavaScript (ES6), LD = 103 ( crackato )

Usare una distanza Levenshtein così alta non è probabilmente la strategia migliore, ma proviamoci comunque.

n => { for(i = 0, str = ''; i <= n; i++) { str += +[...i * i + ''].reverse().join(''); } return str[n]; }

Casi test

Soluzione prevista

$=>eval(atob`Wy4uLkFycmF5KCQrMSldLm1hcCgoXyxpKT0+K1suLi5pKmkrJyddLnJldmVyc2UoKS5qb2luYGApLmpvaW5gYFskXQ`)

Parte codificata:

[...Array($+1)].map((_,i)=>+[...i*i+''].reverse().join``).join``[$]

Questa striscia porta gli 0 dopo l'inversione?
Erik the Outgolfer,

@EriktheOutgolfer Sì. L'unario +assicura che la stringa invertita sia forzata su un numero.
Arnauld,

1
Ohhhh ... wow, è più difficile di quanto pensassi.
Magic Octopus Urn


3

Python 2, 104 byte, LD = 21 Invalid AND Cracked

d=lambda y:y if'0'!=str(y)[-1]else d(y/10)
lambda n:''.join([str(d(x*x))[::-1]for x in range(1,n)])[n-1]

PS È consentita una quantità illimitata di spazi bianchi e commenti? Se è così, questo non sarà difficile da decifrare.


1
"Il codice non rivelato non può essere più lungo della soluzione originale."
Khuldraeseth na'Barya,

@Scrooble L'ho visto ma non credo che sarà difficile realizzare un programma molto più breve di questo. Già fatto in Python 3 in un'altra risposta e funziona in Python 2, quindi tutto ciò che devono aggiungere è un gruppo di \ns (circa 50)
dylnan,

50 newline non supererebbero una distanza di Levenshtein di 21?
Khuldraeseth na'Barya,

@Scrooble Hai perfettamente ragione che mi sono confuso e stavo pensando al problema in modo errato. Grazie
dylnan il

1
Questo sembra errore su input inferiori a 5
Leo,

3

6502 codice macchina (C64), LD = 1 ( incrinato )

00 C0 20 FD AE A0 00 99 5B 00 C8 20 73 00 90 F7 99 5B 00 A2 0B CA 88 30 09 B9
5B 00 29 0F 95 5B 10 F3 A9 00 95 5B CA 10 F9 A9 00 A0 03 99 69 00 88 10 FA A0
20 A2 76 18 B5 E6 90 02 09 10 4A 95 E6 E8 10 F4 A2 03 76 69 CA 10 FB 88 F0 11
A2 09 B5 5C C9 08 30 04 E9 03 95 5C CA 10 F3 30 D6 A2 03 B5 69 95 57 CA 10 F9
A9 01 85 FB A2 03 A9 00 95 FB CA D0 FB A2 03 B5 FB 95 22 95 26 CA 10 F7 A9 00
A2 03 95 69 CA 10 FB A0 20 A2 02 46 25 76 22 CA 10 FB 90 0C A2 7C 18 B5 AA 75
ED 95 ED E8 10 F7 A2 7D 06 26 36 AA E8 10 FB 88 10 DD A0 0B A9 00 99 5A 00 88
D0 FA A0 20 A2 09 B5 5C C9 05 30 04 69 02 95 5C CA 10 F3 06 69 A2 FD 36 6D E8
D0 FB A2 09 B5 5C 2A C9 10 29 0F 95 5C CA 10 F4 88 D0 D7 E0 0A F0 05 E8 B5 5B
F0 F7 09 30 99 5B 00 C8 E8 E0 0B F0 04 B5 5B 90 F1 88 B9 5B 00 C9 30 F0 F8 A2
7C 18 B5 DB E9 00 95 DB E8 10 F7 90 14 88 30 05 B9 5B 00 D0 EA A2 7C F6 7F D0
03 E8 10 F9 4C 73 C0 B9 5B 00 4C D2 FF

Demo online , utilizzo:sys49152,ndov'ènl'input indicizzato 0.


Per l'ultimo caso di test, hai bisogno di un po 'di pazienza, poiché questa povera macchina deve fare milioni di bit-shift e aggiunte per presentarti il ​​risultato;)

Il linguaggio qui è il codice macchina, quindi LD viene misurato in questo formato - tuttavia, per dare qualcosa per cominciare, ecco il programma nel ca65sorgente dell'assemblatore:

NUMSIZE         = 4     ; 32 bit integers ...
NUMSTRSIZE      = 11    ; need up to 11 characters for 0-terminated string

.segment "ZPUSR": zeropage
v_x:            .res    NUMSIZE         ; next number to be squared

.segment "ZPFAC": zeropage
v_n:            .res    NUMSIZE         ; input index (0-based), counts down
nc_string:      .res    NUMSTRSIZE      ; string buffer for numbers

.segment "ZPTMP": zeropage
mpm_arg1:       .res    NUMSIZE         ; arg1 for multiplication
mpm_arg2:       .res    NUMSIZE         ; arg2 for multiplication

.segment "ZPFAC2": zeropage
mpm_res:        .res    NUMSIZE         ; numeric result (mult and str convert)

; load address for creating a C64 .PRG file:

.segment "LDADDR"
                .word   $c000

.code

; first read number from command argument and convert to unsigned
; integer in little-endian:

                jsr     $aefd
                ldy     #$00
rn_loop:        sta     nc_string,y
                iny
                jsr     $73
                bcc     rn_loop
                sta     nc_string,y
                ldx     #NUMSTRSIZE
stn_copybcd:    dex
                dey
                bmi     stn_fillzero
                lda     nc_string,y
                and     #$f
                sta     nc_string,x
                bpl     stn_copybcd
stn_fillzero:   lda     #$0
                sta     nc_string,x
                dex
                bpl     stn_fillzero
                lda     #$0
                ldy     #(NUMSIZE-1)
stn_znumloop:   sta     mpm_res,y
                dey
                bpl     stn_znumloop
                ldy     #(NUMSIZE*8)
stn_loop:       ldx     #($81-NUMSTRSIZE)
                clc
stn_rorloop:    lda     nc_string+NUMSTRSIZE+$80,x
                bcc     stn_skipbit
                ora     #$10
stn_skipbit:    lsr     a
                sta     nc_string+NUMSTRSIZE+$80,x
                inx
                bpl     stn_rorloop
                ldx     #(NUMSIZE-1)
stn_ror:        ror     mpm_res,x
                dex
                bpl     stn_ror
                dey
                beq     main
stn_sub:        ldx     #(NUMSTRSIZE-2)
stn_subloop:    lda     nc_string+1,x
                cmp     #$8
                bmi     stn_nosub
                sbc     #$3
                sta     nc_string+1,x
stn_nosub:      dex
                bpl     stn_subloop
                bmi     stn_loop

main:
                ldx     #(NUMSIZE-1)
argloop:        lda     mpm_res,x
                sta     v_n,x
                dex
                bpl     argloop
                lda     #$01
                sta     v_x
                ldx     #(NUMSIZE-1)
                lda     #$00
initxloop:      sta     v_x,x
                dex
                bne     initxloop

mainloop:

; prepare arguments for multiplication:

                ldx     #(NUMSIZE-1)
sqrargloop:     lda     v_x,x
                sta     mpm_arg1,x
                sta     mpm_arg2,x
                dex
                bpl     sqrargloop

; do multiplication:

                lda     #$00
                ldx     #(NUMSIZE-1)
mul_clearloop:  sta     mpm_res,x
                dex
                bpl     mul_clearloop
                ldy     #(NUMSIZE*8)
mul_loop:       ldx     #(NUMSIZE-2)
                lsr     mpm_arg1+NUMSIZE-1
mul_rorloop:    ror     mpm_arg1,x
                dex
                bpl     mul_rorloop
                bcc     mul_noadd
                ldx     #($80-NUMSIZE)
                clc
mul_addloop:    lda     mpm_arg2+NUMSIZE+$80,x
                adc     mpm_res+NUMSIZE+$80,x
                sta     mpm_res+NUMSIZE+$80,x
                inx
                bpl     mul_addloop
mul_noadd:      ldx     #($81-NUMSIZE)
                asl     mpm_arg2
mul_rolloop:    rol     mpm_arg2+NUMSIZE+$80,x
                inx
                bpl     mul_rolloop
                dey
                bpl     mul_loop

; convert result to string:

                ldy     #NUMSTRSIZE
                lda     #$0
nts_fillzero:   sta     nc_string-1,y
                dey
                bne     nts_fillzero
                ldy     #(NUMSIZE*8)
nts_bcdloop:    ldx     #(NUMSTRSIZE-2)
nts_addloop:    lda     nc_string+1,x
                cmp     #$5
                bmi     nts_noadd
                adc     #$2
                sta     nc_string+1,x
nts_noadd:      dex
                bpl     nts_addloop
                asl     mpm_res
                ldx     #($ff-NUMSIZE+2)
nts_rol:        rol     mpm_res+NUMSIZE,x       ; + $100 w/o zp wraparound
                inx
                bne     nts_rol
                ldx     #(NUMSTRSIZE-2)
nts_rolloop:    lda     nc_string+1,x
                rol     a
                cmp     #$10
                and     #$f
                sta     nc_string+1,x
nts_rolnext:    dex
                bpl     nts_rolloop
                dey
                bne     nts_bcdloop
nts_scan:       cpx     #(NUMSTRSIZE-1)
                beq     nts_copydigits
                inx
                lda     nc_string,x
                beq     nts_scan
nts_copydigits: ora     #$30
                sta     nc_string,y
                iny
                inx
                cpx     #(NUMSTRSIZE)
                beq     strip0loop
                lda     nc_string,x
                bcc     nts_copydigits

; search for first non-0 character from the end of the string:

strip0loop:     dey
                lda     nc_string,y
                cmp     #$30
                beq     strip0loop

; decrement n for each digit:

founddigit:
                ldx     #($80-NUMSIZE)
                clc
decnloop:       lda     v_n+NUMSIZE+$80,x
                sbc     #$00
                sta     v_n+NUMSIZE+$80,x
                inx
                bpl     decnloop
                bcc     foundresult

                dey
                bmi     next_x
                lda     nc_string,y
                bne     founddigit

; increment x to calculate next square number:

next_x:
                ldx     #($80-NUMSIZE)
incxloop:       inc     v_x+NUMSIZE-$80,x
                bne     incxdone
                inx
                bpl     incxloop
incxdone:       jmp     mainloop

foundresult:    lda     nc_string,y
                jmp     $ffd2

... ed ecco lo script del linker per ld65:

MEMORY {
  LDADDR: start = $bffe, size = 2;
  CODE: start = $c000, size = $1000;
  ZPTMP: start = $0022, size = $0008;
  ZPFAC: start = $0057, size = $000f;
  ZPFAC2: start = $0069, size = $0004;
  ZPUSR: start = $00fb, size = $0004;
}

SEGMENTS {
  LDADDR: load = LDADDR;
  CODE: load = CODE;
  ZPTMP: load = ZPTMP, type = zp;
  ZPFAC: load = ZPFAC, type = zp;
  ZPFAC2: load = ZPFAC2, type = zp;
  ZPUSR: load = ZPUSR, type = zp;
}

incrinato (credo)
Jo.

@Jo. si, modificato.
Felix Palmen,


2

Java 8, (177 byte) LD = 92 ( Cracking di @Arnauld )

( Ho usato questo calcolatore LD online. )

n->{String r="",t=r;for(int i=1,j;r.length()<=n+1;i++)if(Math.sqrt(i)%1==0){for(t="",j=(i+"").length();j>0;t+=(i+"").charAt(--j));r+=t.replaceAll("^0+","");}return r.charAt(n);}

Questo probabilmente non è troppo difficile se si semplicemente golf questo. :)

Spiegazione:

Provalo qui.

n->{                             // Method with integer parameter and character return-type
  String r="",                   //  Result-String, starting empty
         t=r;                    //  Temp-String, starting empty
  for(int i=1,j;                 //  Index-integers
      r.length()<=n+1;i++)       //  Loop (1) as long as the length is at least n+1
    if(Math.sqrt(i)%1==0){       //   If the current number `i` is a perfect square:
      for(t="",                  //    Reset the temp-String to empty
          j=(i+"").length();     //    Set `j` to the length of the current number
          j>0;                   //    Inner loop (2) as long as `j` is larger than 0
        t+=                      //     Append the temp-String with:
           (i+"").charAt(--j)    //     The digit of integer `i` at index `j-1`
                                 //     (by first decrease `j` with 1 with `--j`)
      );                         //    End of inner loop (2)
      r+=t                       //    And then append the temp-String to the result-String
          .replaceAll("^0+","");}//    after we've removed any leading zeroes
                                 //  End of loop (1) (implicit / single-line body)
  return r.charAt(n);            //  Return the `n`'th character of the result-String
}                                // End of method


Soluzione n->{String r="";for(int i=1;r.length()<=n+1;r+=new Long(new StringBuffer(i*i+++"").reverse()+""));return r.charAt(n);}
prevista


2

6502 codice macchina (C64), LD = 1, sicuro

00 C0 20 FD AE A0 00 99 5B 00 C8 20 73 00 90 F7 99 5B 00 A2 0B CA 98 88 30 09
B9 5B 00 29 0F 95 5B 10 F2 95 5B CA 10 FB A0 20 A2 76 18 B5 E6 90 02 09 10 4A
95 E6 E8 10 F4 A2 03 76 69 CA 10 FB 88 F0 11 A2 09 B5 5C C9 08 30 04 E9 03 95
5C CA 10 F3 30 D6 A2 03 B5 69 95 57 CA 10 F9 A9 01 85 FB A2 03 A9 00 95 FB CA
D0 FB A2 03 B5 FB 95 22 95 26 CA 10 F7 A9 00 A2 03 95 69 CA 10 FB A0 20 A2 02
46 25 76 22 CA 10 FB 90 0C A2 7C 18 B5 AA 75 ED 95 ED E8 10 F7 A2 7D 06 26 36
AA E8 10 FB 88 10 DD A2 0B A9 00 95 5A CA D0 FB A0 20 A2 09 B5 5C C9 05 30 04
69 02 95 5C CA 10 F3 06 69 A2 FD 36 6D E8 D0 FB A2 09 B5 5C 2A C9 10 29 0F 95
5C CA 10 F4 88 D0 D7 E8 B5 5B F0 FB 09 30 99 5B 00 C8 E8 E0 0B F0 04 B5 5B 90
F1 88 B9 5B 00 C9 30 F0 F8 A2 7C 18 B5 DB E9 00 95 DB E8 10 F7 90 14 88 30 05
B9 5B 00 D0 EA A2 7C F6 7F D0 03 E8 10 F9 4C 68 C0 B9 5B 00 4C D2 FF

Demo online , utilizzo:sys49152,ndov'ènl'input indicizzato 0.


Soluzione prevista: (diff)

 B9 5B 00 29 0F 95 5B 10 F2 95 5B CA 10 FB A0 20 A2 76 18 B5 E6 90 02 09 10 4A
-95 E6 E8 10 F4 A2 03 76 69 CA 10 FB 88 F0 11 A2 09 B5 5C C9 08 30 04 E9 03 95
+95 E6 E8 10 F4 A2 03 76 69 CA 10 FB 88 F0 11 A2 09 B5 5C C9 08 90 04 E9 03 95
 5C CA 10 F3 30 D6 A2 03 B5 69 95 57 CA 10 F9 A9 01 85 FB A2 03 A9 00 95 FB CA

Il 30(opcode bmi) è sostituito da 90(opcode bcc). Ciò corrisponde alla parte seguente nell'origine dell'assemblatore:

stn_subloop:    lda     nc_string+1,x
                cmp     #$8
                bmi     stn_nosub       ; use bcc here for same result
                sbc     #$3
                sta     nc_string+1,x

Funziona perché questo codice controlla se un numero è inferiore a 8. Il cmp istruzione esegue una sottrazione per questo, impostando i flag di conseguenza. Quindi, se l'accumulatore contiene un numero inferiore a 8, questo trabocca, cancellando la bandiera carry, quindi l'istruzione di diramazione corretta è effettivamente bcc. bmi(ramificazione quando negativo), come nel codice originale, funziona anche qui, perché i numeri confrontati sono abbastanza piccoli, quindi il risultato della sottrazione finisce nell'intervallo negativo ( $80-$ff) quando si verifica un underflow.

Demo online


Questa è una versione migliorata / compatta di mia precedente presentazione . Tra gli altri trucchi per ridurre le dimensioni, rimuove il codice inutile che era contenuto e consentiva una sorta di "semplice" *) crack. Tutto sommato, la dimensione è ridotta di 16 byte. Questa volta, dovrebbe essere un po 'più difficile trovare il programma equivalente con LD 1 :)

*) probabilmente ancora del lavoro da trovare, ovviamente :)

Ancora una volta, ecco l' ca65origine dell'assemblatore, per aiutare a iniziare con il codice:

NUMSIZE         = 4     ; 32 bit integers ...
NUMSTRSIZE      = 11    ; need up to 11 characters for 0-terminated string

.segment "ZPUSR": zeropage
v_x:            .res    NUMSIZE         ; next number to be squared

.segment "ZPFAC": zeropage
v_n:            .res    NUMSIZE         ; input index (0-based), counts down
nc_string:      .res    NUMSTRSIZE      ; string buffer for numbers

.segment "ZPTMP": zeropage
mpm_arg1:       .res    NUMSIZE         ; arg1 for multiplication
mpm_arg2:       .res    NUMSIZE         ; arg2 for multiplication

.segment "ZPFAC2": zeropage
mpm_res:        .res    NUMSIZE         ; numeric result (mult and str convert)

; load address for creating a C64 .PRG file:

.segment "LDADDR"
                .word   $c000

.code

; first read number from command argument and convert to unsigned
; integer in little-endian:

                jsr     $aefd
                ldy     #$00
rn_loop:        sta     nc_string,y
                iny
                jsr     $73
                bcc     rn_loop
                sta     nc_string,y
                ldx     #NUMSTRSIZE
stn_copybcd:    dex
                tya
                dey
                bmi     stn_fillzero
                lda     nc_string,y
                and     #$f
                sta     nc_string,x
                bpl     stn_copybcd
stn_fillzero:   sta     nc_string,x
                dex
                bpl     stn_fillzero
                ldy     #(NUMSIZE*8)
stn_loop:       ldx     #($81-NUMSTRSIZE)
                clc
stn_rorloop:    lda     nc_string+NUMSTRSIZE+$80,x
                bcc     stn_skipbit
                ora     #$10
stn_skipbit:    lsr     a
                sta     nc_string+NUMSTRSIZE+$80,x
                inx
                bpl     stn_rorloop
                ldx     #(NUMSIZE-1)
stn_ror:        ror     mpm_res,x
                dex
                bpl     stn_ror
                dey
                beq     main
stn_sub:        ldx     #(NUMSTRSIZE-2)
stn_subloop:    lda     nc_string+1,x
                cmp     #$8
                bmi     stn_nosub
                sbc     #$3
                sta     nc_string+1,x
stn_nosub:      dex
                bpl     stn_subloop
                bmi     stn_loop

main:
                ldx     #(NUMSIZE-1)
argloop:        lda     mpm_res,x
                sta     v_n,x
                dex
                bpl     argloop
                lda     #$01
                sta     v_x
                ldx     #(NUMSIZE-1)
                lda     #$00
initxloop:      sta     v_x,x
                dex
                bne     initxloop

mainloop:

; prepare arguments for multiplication:

                ldx     #(NUMSIZE-1)
sqrargloop:     lda     v_x,x
                sta     mpm_arg1,x
                sta     mpm_arg2,x
                dex
                bpl     sqrargloop

; do multiplication:

                lda     #$00
                ldx     #(NUMSIZE-1)
mul_clearloop:  sta     mpm_res,x
                dex
                bpl     mul_clearloop
                ldy     #(NUMSIZE*8)
mul_loop:       ldx     #(NUMSIZE-2)
                lsr     mpm_arg1+NUMSIZE-1
mul_rorloop:    ror     mpm_arg1,x
                dex
                bpl     mul_rorloop
                bcc     mul_noadd
                ldx     #($80-NUMSIZE)
                clc
mul_addloop:    lda     mpm_arg2+NUMSIZE+$80,x
                adc     mpm_res+NUMSIZE+$80,x
                sta     mpm_res+NUMSIZE+$80,x
                inx
                bpl     mul_addloop
mul_noadd:      ldx     #($81-NUMSIZE)
                asl     mpm_arg2
mul_rolloop:    rol     mpm_arg2+NUMSIZE+$80,x
                inx
                bpl     mul_rolloop
                dey
                bpl     mul_loop

; convert result to string:

                ldx     #NUMSTRSIZE
                lda     #$0
nts_fillzero:   sta     nc_string-1,x
                dex
                bne     nts_fillzero
                ldy     #(NUMSIZE*8)
nts_bcdloop:    ldx     #(NUMSTRSIZE-2)
nts_addloop:    lda     nc_string+1,x
                cmp     #$5
                bmi     nts_noadd
                adc     #$2
                sta     nc_string+1,x
nts_noadd:      dex
                bpl     nts_addloop
                asl     mpm_res
                ldx     #($ff-NUMSIZE+2)
nts_rol:        rol     mpm_res+NUMSIZE,x       ; + $100 w/o zp wraparound
                inx
                bne     nts_rol
                ldx     #(NUMSTRSIZE-2)
nts_rolloop:    lda     nc_string+1,x
                rol     a
                cmp     #$10
                and     #$f
                sta     nc_string+1,x
nts_rolnext:    dex
                bpl     nts_rolloop
                dey
                bne     nts_bcdloop
nts_scan:       inx
                lda     nc_string,x
                beq     nts_scan
nts_copydigits: ora     #$30
                sta     nc_string,y
                iny
                inx
                cpx     #(NUMSTRSIZE)
                beq     strip0loop
                lda     nc_string,x
                bcc     nts_copydigits

; search for first non-0 character from the end of the string:

strip0loop:     dey
                lda     nc_string,y
                cmp     #$30
                beq     strip0loop

; decrement n for each digit:

founddigit:
                ldx     #($80-NUMSIZE)
                clc
decnloop:       lda     v_n+NUMSIZE+$80,x
                sbc     #$00
                sta     v_n+NUMSIZE+$80,x
                inx
                bpl     decnloop
                bcc     foundresult

                dey
                bmi     next_x
                lda     nc_string,y
                bne     founddigit

; increment x to calculate next square number:

next_x:
                ldx     #($80-NUMSIZE)
incxloop:       inc     v_x+NUMSIZE-$80,x
                bne     incxdone
                inx
                bpl     incxloop
incxdone:       jmp     mainloop

foundresult:    lda     nc_string,y
                jmp     $ffd2

... ed ecco lo script del linker per ld65:

MEMORY {
  LDADDR: start = $bffe, size = 2;
  CODE: start = $c000, size = $1000;
  ZPTMP: start = $0022, size = $0008;
  ZPFAC: start = $0057, size = $000f;
  ZPFAC2: start = $0069, size = $0004;
  ZPUSR: start = $00fb, size = $0004;
}

SEGMENTS {
  LDADDR: load = LDADDR;
  CODE: load = CODE;
  ZPTMP: load = ZPTMP, type = zp;
  ZPFAC: load = ZPFAC, type = zp;
  ZPFAC2: load = ZPFAC2, type = zp;
  ZPUSR: load = ZPUSR, type = zp;
}

Consideralo incrinato :)
Jo.

@Jo. bene se insisti - grazie mille, l'ho modificato.
Felix Palmen,





0

C ++, LD = 159

0 indicizzato, immesso argv[1], compilato su GCC 7.2.0

#import<bits/stdc++.h>
char*h,b[1<<17],*q=b;int x,y;main(int,char**j){sscanf(j[1],"%d",&y);do{x++;q+=sprintf(h=q,"%d",x*x);while(*--q==48);std::reverse(h,++q);}while(q-b<=y);b[y+1]=0,printf(b+y);}

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.