Este é um número Calvin candidato?

27

Este desafio é uma homenagem ao nosso Legendary Challenge Writer ™, o Calvin's Hobbies - agora renomeado para Helka Homba -, no mesmo espírito que o Generate Dennis Numbers .

Calvin é um colaborador impressionante do PPCG, com a 6ª maior reputação no geral e provavelmente o melhor desafio de escrever entre todos nós. No entanto, é claro, para esse desafio, focaremos no ID do usuário.

26997 pode não parecer muito interessante a princípio. De fato, é quase interessante de algumas maneiras. Por exemplo, aqui está um gráfico 26997 mod <n>para determinados valores de n:

n   |  26997 % n
----+-----------
3   |  0
4   |  1
5   |  2
6   |  3
7   |  5 :(
8   |  5
9   |  6
10  |  7

No entanto, 26997 é um dos poucos números que podem ser representados por , onde é um número inteiro> 0.(n * 10)n - nn

Aqui estão os primeiros números que podem ser expressos dessa maneira, que chamaremos daqui em diante de Números de Calvino :

9
398
26997
2559996
312499995
46655999994
8235429999993
1677721599999992
387420488999999991
99999999999999999990
28531167061099999999989
8916100448255999999999988
3028751065922529999999999987
1111200682555801599999999999986
437893890380859374999999999999985
184467440737095516159999999999999984
82724026188633676417699999999999999983
39346408075296537575423999999999999999982
19784196556603135891239789999999999999999981
10485759999999999999999999999999999999999999980

Estes números de Calvin têm algumas propriedades interessantes. Mais padrões surgem quando os alinhamos à direita e realçamos todos os 9s:

captura de tela

Os que nos interessam para esse desafio são:

  • Independentemente disso n, todo número Calvin termina com .10n - n

    Assim, Calvin (1) com extremidades 9, Calvin (2) com as extremidades 98, e o padrão continua 997, 9996, 99995, etc., com cada sucessiva Número Calvin contagem regressiva e adicionando um extra 9para o início.

  • Para valores de nonde n % 10 == 0(ou seja, né divisível por 10), Calvin (n) termina com .102n - n

    Ou seja, o padrão se estende por duas vezes mais dígitos que o normal, com um número extra de 9s no início igual a n.

  • Quando né uma potência de 10( 10, 100, 1000, etc.), o padrão se estende ainda mais, cada dígito é qualquer um 9ou uma 0.

    Esse padrão é o seguinte: noves e zeros. Isso é mais fácil de entender em um gráfico (sua solução só precisará manipular números de até 10000, de modo que é tudo o que você precisa):(n + 1) * 10n - nn

    n      |  Calvin(n)
    -------+-----------------------
    10     |  19 nines, 1 zero
    100    |  298 nines, 2 zeroes
    1000   |  3997 nines, 3 zeroes
    10000  |  49998 nines, 4 zeroes
    

    O número de noves até exibe várias propriedades do próprio Calvin Numbers , mas há muitos detalhes para esse desafio.

Desafio

Os números de Calvin ficam grandes demais, muito rapidamente, para que um desafio "obtenha o enésimo número de calvin seja viável em idiomas sem números inteiros de precisão arbitrária. Portanto, o desafio é determinar se um número se encaixa nos padrões acima - isto é, se um número é um "número Calvin candidato" ou não.

Aqui estão os critérios para um número ser considerado um número Calvin candidato (daqui em diante referido como um CCN para abreviar):

  • Termina com um número que se ajusta ao padrão para um número inteiro .10n - nn

    Portanto, para ser um CCN, um número deve terminar com 9 ou 98 ou 997, 9996, 99995 etc.

  • Se o último dígito for 0, também deve terminar com , da mesma forma que no ponto anterior.102n - nn

    Isso significa que 12312312399999999999999999999999999999999999980não é um CCN, mas 10485759999999999999999999999999999999999999980é (é o correto, de fato).

  • Se o valor das nduas etapas anteriores for uma potência de 10, o número inteiro deverá se encaixar no terceiro padrão descrito acima.

Entrada / Saída

A entrada será fornecida como uma sequência e sempre representará um número menor que Calvin(10000) + 10000(que também pode ser expresso como ). (Para esclarecer, a maior entrada possível é 50000 noves e a menor entrada possível .)10500001

A saída deve ser um valor verdadeiro se a entrada representar um número que é um CCN e, caso contrário, um valor falso. Para as definições desses termos, consulte meta .

Casos de teste

Entradas que devem resultar em um valor de verdade:

9
26997
99999999999999999990
437893890380859374999999999999985
10485759999999999999999999999999999999999999980
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999900
259232147948794494594485446818048254863271026096382337884099237269509380022108148908589797968903058274437782549758243999867043174477180579595714249308002763427793979644775390624999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999850
1027092382693614216458468213549848274267264533712122209400214436472662418869004625362768700557725707157332451380426829473630485959339004149867738722096608953864534215649211386152032635755501464142277508289403434891444020975243742942368836579910208098242623061684967794815600266752580663281483595687307649904776800899000484103534573979334062832465904049046104660220505973505050538180250643437654409375728443182380726453925959886901573523090619465866810938078629561306599174923972607310649219442207992951278588892681161967770532314854195892941913447519131828356181219857012229150315613569162930098836696593474888020746503116685472977764615483225628639443918309216648893055765917642528801571387940219884056021782642758517893124803355573565644666880920219871370649806723296262307899148031362558110611562055614190049332906933360406981359187305353360484377948591528385990255894034369523166777375785900198782250651053530165824984161319460372145229568890321167955690544235365954748429659526071133879976348254667755220636244075595290123987745560038255541751251200827018722242010925729483977388235141539109139120069464709993781356334885359200734157439642935779132120725231008699003342908280056975158266782782304550273268246184659474285971272532354920744956064671379745219778013465792544241259691493098443741845166419905920702654683993902052727208789915748213660571390107102976665776293366616518962323688316843422737162297255648351087284877987537325761187239807598009767936409247247417410607537333841650998421607775989879490006136112078031237742552602618996017404602674987181629319060214150458746352191115606789019875790921190573561400752476956787515392210098071407806221412149732955903681690377998882038499470092453400748916257640501488510563314141992573250882286817352407459053866180642034662845694338400386823496563185664221362457851894843439705365082614359220653285052800751906334000698723288454227654466240011140570190301931122357632719033275258503935182047714841766010764632214069382579660602964184231995352310981811428980530707871661256260926759509418970021224649566130995825802676411575264295689037775857674060557127369881379685432291930869072749065675720647595081516460449973211035071920099349836074945813885239767788449030051892470053308048906746273036871919251738920141071153777908913021898541658119513188402271468288293408246833819954990709460114510017598873554406350044072275643892449218394225569069468466660333869360644718801813500285081977089623921689922204185138003164149106921903053243405307546841149889662566529697217181329051855403329741409045760789280950603184354320839342588593832348459938736210265795978675460906504449491132656307256451707333439200130425932724262464823848348296787445624028385464112471408499986690593095395244034885421580844176161027627954578726208600199909963055422192706751708210693468639072881081717288837393188012794669089175022406897622823484220002211676520484520241135615999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999028

Entradas que devem resultar em um valor falso:

1
26897
79999999999999999990
437893890380859374299999999999985
12312312399999999999999999999999999999999999980
999998999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999900
259232147948794494594485446818048254863271026096382337884099237269509380022108148908589797968903058274437782549758243999867043174477180579595714249308002763427793979644775390624999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999911111
1027092382693614216458468213549848274267264533712122209400214436472662418869004625362768700557725707157332451380426829473630485959339004149867738722096608953864534215649211386152032635755501464142277508289403434891444020975243742942368836579910208098242623061684967794815600266752580663281483595687307649904776800899000484103534573979334062832465904049046104660220505973505050538180250643437654409375728443182380726453925959886901573523090619465866810938078629561306599174923972607310649219442207992951278588892681161967770532314854195892941913447519131828356181219857012229150315613569162930098836696593474888020746503116685472977764615483225628639443918309216648893055765917642528801571387940219884056021782642758517893124803355573565644666880920219871370649806723296262307899148031362558110611562055614190049332906933360406981359187305353360484377948591528385990255894034369523166777375785900198782250651053530165824984161319460372145229568890321167955690544235365954748429659526071133879976348254667755220636244075595290123987745560038255541751251200827018722242010925729483977388235141539109139120069464709993781356334885359200734157439642935779132120725231008699003342908280056975158266782782304550273268246184659474285971272532354920744956064671379745219778013465792544241259691493098443741845166419905920702654683993902052727208789915748213660571390107102976665776293366616518962323688316843422737162297255648351087284877987537325761187239807598009767936409247247417410607537333841650998421607775989879490006136112078031237742552602618996017404602674987181629319060214150458746352191115606789019875790921190573561400752476956787515392210098071407806221412149732955903681690377998882038499470092453400748916257640501488510563314141992573250882286817352407459053866180642034662845694338400386823496563185664221362457851894843439705365082614359220653285052800751906334000698723288454227654466240011140570190301931122357632719033275258503935182047714841766010764632214069382579660602964184231995352310981811428980530707871661256260926759509418970021224649566130995825802676411575264295689037775857674060557127369881379685432291930869072749065675720647595081516460449973211035071920099349836074945813885239767788449030051892470053308048906746273036871919251738920141071153777908913021898541658119513188402271468288293408246833819954990709460114510017598873554406350044072275643892449218394225569069468466660333869360644718801813500285081977089623921689922204185138003164149106921903053243405307546841149889662566529697217181329051855403329741409045760789280950603184354320839342588593832348459938736210265795978675460906504449491132656307256451707333439200130425932724262464823848348296787445624028385464112471408499986690593095395244034885421580844176161027627954578726208600199909963055422192706751708210693468639072881081717288837393188012794669089175022406897622823484220002211676520484520241135615999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999027

Regras

  • Você não pode , em nenhum momento do seu programa, manipular números inteiros maiores que 18446744073709551615( ), se o seu idioma suportar números inteiros de precisão arbitrária (ou tipos de números com uma precisão alta o suficiente para permitir o armazenamento de números maiores que isso).264

    Isso é simplesmente para evitar soluções que passam por todos os números possíveis de Calvin (ou todos os valores possíveis de ).10n - n

  • Isso é , então o código mais curto em bytes vencerá.

Maçaneta da porta
fonte
"Se o valor de n nas duas etapas anteriores for uma potência de 10, o número inteiro deverá se encaixar no terceiro padrão descrito acima." A que se refere o 'terceiro padrão'?
feersum
@feersum Há uma lista com marcadores de três coisas - é a última.
Maçaneta
Não entendo por que o penúltimo caso de teste falso é falso. Que regra viola?
Alexis King
@AlexisKing Boa captura; qualquer coisa que termine 9deve ser verdadeira. Fixo.
Maçaneta
@ Doorknob Mesmo com essa alteração, o número ainda parece se encaixar nos critérios. Um número que termina em 845 não deveria ter 152 noves? Parece ter mais do que suficiente. Deveria haver metade do número?
Alexis King

Respostas:

8

Raquete, 353

(require srfi/13)(let([s(~a(read))])(for/or([n(range 1 999)])(and(let*([y(string-length(~a n))])(string-suffix?(string-append(make-string(-(if(=(modulo n 10)0)(* 2 n)n)y)#\9)(~r #:min-width y #:pad-string"0"(-(expt 10 y)n)))s))(let([n(inexact->exact(/(log n)(log 10)))])(or(not(integer? n))(string-prefix?(make-string(-(*(+ 1 n)(expt 10 n))n)#\9)s))))))

Aceita um número de stdin, outputs #tou #f.

Versão não destruída:

(require srfi/13)

(define (calvin? str)
  (for/or ([n (in-range 1 10001)])
    (and (10^n-n$? n str)
         (or (not (integer? (/ (log n) (log 10))))
             (expt-of-ten-check? n str)))))

(define (10^n-n$? n str)
  (let* ([div-by-ten? (zero? (modulo n 10))]
         [digits (string-length (~a n))]
         [nines (- (if div-by-ten? (* 2 n) n) digits)]
         [suffix (string-append (make-string nines #\9)
                                (~r #:min-width digits #:pad-string "0" (- (expt 10 digits) n)))])
    (string-suffix? suffix str)))

(define (expt-of-ten-check? n str)
  (let* ([n (inexact->exact (/ (log n) (log 10)))]
         [nines (- (* (add1 n) (expt 10 n)) n)]
         [prefix (make-string nines #\9)])
    (string-prefix? prefix str)))

Normalmente eu não pratico golfe com código, e o Racket certamente não é o idioma mais adequado para ele, mas ninguém respondeu ainda, então pensei em tentar. ;)

Alexis King
fonte
Eles podem estar esperando que eu responda, mas, dado o meu histórico de postagens, é provavelmente melhor que você não tenha esperado;)
Calvin's Hobbies