这是候选人卡尔文数吗?


27

这项挑战是对我们的传奇挑战作家( Calvin's Hobbies)(现在更名为Helka Homba)的致敬,其精神与“ 生成丹尼斯数字 ”相同 。

卡尔文(Calvin)是PPCG的杰出贡献者,在整体声誉中名列第六,在我们所有人当中,毫无疑问,最佳挑战写作技能是无可争议的。但是,当然,对于这一挑战,我们将重点关注他的用户ID。

起初26997可能看起来不太有趣。实际上, 从几个方面来说,这几乎是有趣的。例如,这是26997 mod <n>某些值的图表n

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

但是,26997是可以用表示的少数几个数字之一,其中整数> 0。(n * 10)n - nn

这是可以用这种方式表示的前几个数字,此后我们将其称为加尔文数字

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

这些卡尔文数具有一些有趣的属性。当我们右对齐并突出显示所有9s 时,会出现更多模式:

屏幕截图

我们对此挑战感兴趣的是:

  • 无论如何n,每个加尔文数都以结尾 。10n - n

    所以,卡尔文(1)的端部9,卡尔文(2)与端98,而图案继续997999699995等等,每一个连续的卡尔文数量递减计数并增加一个额外的9到开始处。

  • 对于nwhere的值n % 10 == 0n即被10整除),Calvin(n)以结尾。102n - n

    也就是说,该模式的扩展位数是正常数字的两倍,且9开头的s 数量等于n

  • n是的功率10101001000等),该图案进一步-每单位可以是一个延伸9或一个0

    该模式如下: 9和 0。这在图表中更容易理解(无论如何,您的解决方案只需要处理最多10000个数字,这就是您所需要的):(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
    

    九分的数字甚至表现出加尔文数字 本身的几个属性,但这对于这个挑战来说太过分了。

挑战

卡尔文数变得太大,太快,以至于“使第n个卡尔文数挑战在没有任意精度整数的语言中可行。因此,挑战在于确定数字是否适合上述模式,即是否适合一个数字是否为“候选加尔文数字”。

以下是将号码视为候选加尔文号码 (以下简称为CCN)的标准:

  • 它以适合整数的模式结尾的数字 。10n - nn

    因此,要成为CCN,数字必须以9或98或997、9996、99995等结尾。

  • 如果最后一位是0,则它也必须以结束,与 上一点相同。102n - nn

    这意味着它12312312399999999999999999999999999999999999980不是CCN,而是10485759999999999999999999999999999999999999980(实际上是正确的)。

  • 如果n前两个步骤中的值是10的幂,则整数必须适合上述的第三种模式。

输入输出

输入将以字符串形式提供,并且将始终表示小于的数字Calvin(10000) + 10000(也可以表示为 )。(为澄清起见,最大可能的输入是50000个9,最小可能的输入是。)10500001

如果输入表示一个数字,则输出应为真实值,否则为假值。有关这些术语定义,请参见meta

测试用例

应产生真实值的输入:

9
26997
99999999999999999990
437893890380859374999999999999985
10485759999999999999999999999999999999999999980
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999900
259232147948794494594485446818048254863271026096382337884099237269509380022108148908589797968903058274437782549758243999867043174477180579595714249308002763427793979644775390624999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999850
1027092382693614216458468213549848274267264533712122209400214436472662418869004625362768700557725707157332451380426829473630485959339004149867738722096608953864534215649211386152032635755501464142277508289403434891444020975243742942368836579910208098242623061684967794815600266752580663281483595687307649904776800899000484103534573979334062832465904049046104660220505973505050538180250643437654409375728443182380726453925959886901573523090619465866810938078629561306599174923972607310649219442207992951278588892681161967770532314854195892941913447519131828356181219857012229150315613569162930098836696593474888020746503116685472977764615483225628639443918309216648893055765917642528801571387940219884056021782642758517893124803355573565644666880920219871370649806723296262307899148031362558110611562055614190049332906933360406981359187305353360484377948591528385990255894034369523166777375785900198782250651053530165824984161319460372145229568890321167955690544235365954748429659526071133879976348254667755220636244075595290123987745560038255541751251200827018722242010925729483977388235141539109139120069464709993781356334885359200734157439642935779132120725231008699003342908280056975158266782782304550273268246184659474285971272532354920744956064671379745219778013465792544241259691493098443741845166419905920702654683993902052727208789915748213660571390107102976665776293366616518962323688316843422737162297255648351087284877987537325761187239807598009767936409247247417410607537333841650998421607775989879490006136112078031237742552602618996017404602674987181629319060214150458746352191115606789019875790921190573561400752476956787515392210098071407806221412149732955903681690377998882038499470092453400748916257640501488510563314141992573250882286817352407459053866180642034662845694338400386823496563185664221362457851894843439705365082614359220653285052800751906334000698723288454227654466240011140570190301931122357632719033275258503935182047714841766010764632214069382579660602964184231995352310981811428980530707871661256260926759509418970021224649566130995825802676411575264295689037775857674060557127369881379685432291930869072749065675720647595081516460449973211035071920099349836074945813885239767788449030051892470053308048906746273036871919251738920141071153777908913021898541658119513188402271468288293408246833819954990709460114510017598873554406350044072275643892449218394225569069468466660333869360644718801813500285081977089623921689922204185138003164149106921903053243405307546841149889662566529697217181329051855403329741409045760789280950603184354320839342588593832348459938736210265795978675460906504449491132656307256451707333439200130425932724262464823848348296787445624028385464112471408499986690593095395244034885421580844176161027627954578726208600199909963055422192706751708210693468639072881081717288837393188012794669089175022406897622823484220002211676520484520241135615999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999028

输入应导致虚假的值:

1
26897
79999999999999999990
437893890380859374299999999999985
12312312399999999999999999999999999999999999980
999998999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999900
259232147948794494594485446818048254863271026096382337884099237269509380022108148908589797968903058274437782549758243999867043174477180579595714249308002763427793979644775390624999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999911111
1027092382693614216458468213549848274267264533712122209400214436472662418869004625362768700557725707157332451380426829473630485959339004149867738722096608953864534215649211386152032635755501464142277508289403434891444020975243742942368836579910208098242623061684967794815600266752580663281483595687307649904776800899000484103534573979334062832465904049046104660220505973505050538180250643437654409375728443182380726453925959886901573523090619465866810938078629561306599174923972607310649219442207992951278588892681161967770532314854195892941913447519131828356181219857012229150315613569162930098836696593474888020746503116685472977764615483225628639443918309216648893055765917642528801571387940219884056021782642758517893124803355573565644666880920219871370649806723296262307899148031362558110611562055614190049332906933360406981359187305353360484377948591528385990255894034369523166777375785900198782250651053530165824984161319460372145229568890321167955690544235365954748429659526071133879976348254667755220636244075595290123987745560038255541751251200827018722242010925729483977388235141539109139120069464709993781356334885359200734157439642935779132120725231008699003342908280056975158266782782304550273268246184659474285971272532354920744956064671379745219778013465792544241259691493098443741845166419905920702654683993902052727208789915748213660571390107102976665776293366616518962323688316843422737162297255648351087284877987537325761187239807598009767936409247247417410607537333841650998421607775989879490006136112078031237742552602618996017404602674987181629319060214150458746352191115606789019875790921190573561400752476956787515392210098071407806221412149732955903681690377998882038499470092453400748916257640501488510563314141992573250882286817352407459053866180642034662845694338400386823496563185664221362457851894843439705365082614359220653285052800751906334000698723288454227654466240011140570190301931122357632719033275258503935182047714841766010764632214069382579660602964184231995352310981811428980530707871661256260926759509418970021224649566130995825802676411575264295689037775857674060557127369881379685432291930869072749065675720647595081516460449973211035071920099349836074945813885239767788449030051892470053308048906746273036871919251738920141071153777908913021898541658119513188402271468288293408246833819954990709460114510017598873554406350044072275643892449218394225569069468466660333869360644718801813500285081977089623921689922204185138003164149106921903053243405307546841149889662566529697217181329051855403329741409045760789280950603184354320839342588593832348459938736210265795978675460906504449491132656307256451707333439200130425932724262464823848348296787445624028385464112471408499986690593095395244034885421580844176161027627954578726208600199909963055422192706751708210693468639072881081717288837393188012794669089175022406897622823484220002211676520484520241135615999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999027

规则

  • 如果您的语言支持任意精度的整数(或数字类型具有足够高的精度以允许存储大于此的数字),则您可能无法在程序中的任何时候处理大于 18446744073709551615()的整数。264

    这仅仅是为了防止解决方案遍历所有可能的Calvin数(或的所有可能的值)。10n - n

  • 这是,因此以字节为单位的最短代码将获胜。


“如果前两个步骤中n的值是10的幂,则整数必须适合上述的第三种模式。” “第三种模式”指的是什么?
feersum

@feersum这里有三件事的项目符号列表,这是最后一个。
门把手

我不明白为什么倒数第二个错误的测试用例是错误的。它违反什么规则?
亚历克西斯·金

@AlexisKing好收获;任何以结尾结尾的东西都9应该是真实的。固定。
门把手

@Doorknob即使进行了更改,该数字似乎仍然符合标准。以845结尾的数字不应该有152个九分吗?似乎已经足够了。应该是数字的一半吗?
亚历克西斯·金

Answers:


8

球拍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))))))

接受来自stdin,输出#t或的数字#f

非高尔夫版本:

(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)))

我通常不打高尔夫球,Racket当然也不是最适合的语言,但是还没有人回答,所以我认为我会尝试一下。;)


他们可能一直在等我回答,但考虑到我的发帖历史,最好不要等;)
卡尔文的爱好
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.