ลำดับของ Kuznetsov


18

ลำดับของ Kuznetsov

(I made the name up, don't bother with Wikipedia or Google)

ได้รับหมายเลขใด ๆn > 0ให้เป็นตัวแทนของการย้อนกลับของตัวเลขr nทำซ้ำจนกว่าผลลัพธ์สุดท้ายจะเป็นศูนย์ส่งผ่านผลลัพธ์ของการวนซ้ำแต่ละครั้งกลับสู่ฟังก์ชันโดยใช้การเรียกซ้ำหรือวิธีการที่คุณเลือกโดยดำเนินการด้านล่าง:

  • ถ้าการที่ย้ำผลที่ได้คือr > nr % n
  • ถ้าการที่ย้ำผลที่ได้คือn > rn % r
  • หากn % r = 0หรือr % n = 0คุณยุติการทำซ้ำ

รับผลกลางของการกระทำแต่ละครั้งและเก็บไว้ในอาร์เรย์สำหรับคำตอบสุดท้าย หมายเลขเริ่มต้นnไม่ได้เป็นส่วนหนึ่งของลำดับหรือเป็น0; ตัวอย่างควรทำให้ทุกอย่างชัดเจนขึ้น

n=32452345ช่วยให้ใช้เวลาเดินผ่านตัวอย่างเช่นในกรณีที่

54325423 % 32452345 = 21873078 # r > n, uses r % n
87037812 % 21873078 = 21418578 # r > n, uses r % n
87581412 % 21418578 = 1907100  # r > n, uses r % n
1907100 % 17091 = 9999         # n > r, uses n % r
9999 % 9999 = 0                # r % n = n % r = 0, terminated

Result: [21873078, 21418578, 1907100, 9999]     

ตัวอย่างอื่นn=12345678:

87654321 % 12345678 = 1234575 # r > n, uses r % n
5754321 % 1234575 = 816021    # r > n, uses r % n
816021 % 120618 = 92313       # n > r, uses n % r
92313 % 31329 = 29655         # n > r, uses n % r
55692 % 29655 = 26037         # r > n, uses r % n
73062 % 26037 = 20988         # r > n, uses r % n
88902 % 20988 = 4950          # r > n, uses r % n
4950 % 594 = 198              # n > r, uses n % r
891 % 198 = 99                # r > n, uses r % n
99 % 99 = 0                   # r % n = n % r = 0, terminated

Result: [1234575, 816021, 92313, 29655, 26037, 20988, 4950, 198, 99]

ตัวอย่างสุดท้ายn=11000:

11000 % 11 = 0 # n % r = 0, terminated

Result: []

นี่คือการชนะการนับไบต์ต่ำสุดของ


2
สามารถพิมพ์ผลลัพธ์เมื่อการคำนวณเกิดขึ้นหรือต้องสร้างอาร์เรย์หรือไม่
FlipTack

ฉันคิดว่าจะใช้กฎการส่งออกเริ่มต้นเพื่อให้คุณสามารถเลือกรูปแบบผลลัพธ์ (อาร์เรย์ตัวเลขที่แสดงคั่นด้วยช่องว่าง ... )
Luis Mendo

@ Flp.Tkc ฉันจะไม่ จำกัด การส่งออกตราบใดที่ตัวเลขที่ต้องการจะปรากฏขึ้น
Magic Octopus Urn

2
เพียงแค่ทราบว่า 'ย้อนกลับ' ของจำนวนนั้นมีความหมายเฉพาะกับฐานที่เฉพาะเจาะจงเท่านั้น
David Conrad

1
@ Sp3000 ประเภท; ยกเว้นว่าคุณต้องย้อนกลับซ้ำทุกครั้ง คุณเพียงเธรดหมายเลขหนึ่งผ่านการคำนวณไม่ใช่สองและใช้จำนวนที่สองเป็นค่าย้อนกลับของค่าแรกเสมอ
tomsmeding

คำตอบ:



6

PowerShell v2 +, 89 ไบต์

param($n)for(){$r=-join"$n"["$n".length..0];if(!($n=(($r%$n),($n%$r))[$n-gt$r])){exit}$n}

โซลูชั่นซ้ำ ยาวเพราะไม่มีวิธีที่ง่ายที่จะกลับอาร์เรย์ดังนั้นเรา stringify $rมันและดัชนีที่มันย้อนกลับไปยังร้านค้าเข้า จากนั้นจึงหลอกผู้ประกอบการที่จะดึงโมดูโล่ที่เหมาะสมออกมาและจัดเก็บใหม่ใน$nรอบต่อไป อย่างไรก็ตามหากผลลัพธ์เป็นศูนย์นั่นหมายความว่า!($n...)จะเป็น$trueดังนั้นเราจึงexitแทนที่จะ$nแทนตัวเลขที่มีการทิ้งไว้บนท่อและ (โดยปริยาย) กลับมาเป็นอาร์เรย์ แต่ไม่มีห่อหุ้มท่อหรือการบันทึกผลการลงในตัวแปรเริ่มต้นWrite-Outputแท่งขึ้นบรรทัดใหม่ระหว่าง

ลองออนไลน์! (ใช่ตายแล้วร้ายแรง)
PowerShell เปิดตัว TIO แล้ว! คุณต้องให้มันหนึ่งหรือสองวินาทีเพราะ PowerShell เป็นสัตว์ร้ายที่เริ่มต้น แต่ตอนนี้คุณใช่คุณสามารถยืนยันรหัส PowerShell ได้จากเบราว์เซอร์ของคุณ!


Gah เอาชนะฉันไปที่มันและด้วยวิธีการเดียวกัน ดี!
นักเทศน์

6

Perl, 43 38 + 1 = 39 ไบต์

วิ่งด้วย-nธง

say while$_=($;=reverse)>$_?$;%$_:$_%$

ลองออนไลน์! รวมถึงสองตัวอย่างที่ไม่ว่างเปล่า

แผนภูมิคำอธิบาย

-n: while(<>){ ... ;}ห่อโปรแกรมทั้งหมดใน นี่ทำให้โค้ดข้างบนเป็นบรรทัดต่อไปนี้: while(<>){say while$_=($;=reverse)>$_?$;%$_:$_%$;}. แจ้งให้ทราบล่วงหน้าอัฒภาคได้รับการเพิ่มต่อท้ายจึงตอนนี้กลายเป็นตัวอย่างของตัวแปร$ $;ในสภาพของwhileลูปให้<>อ่านหนึ่งบรรทัดของอินพุตโดยอัตโนมัติและบันทึกลงใน$_ตัวแปร ดังนั้นตอนนี้เรามาดูสิ่งที่ล่ามอ่านในวงนอกwhile:

say while$_=($;=reverse)>$_?$;%$_:$_%$;
[op][mod][         condition          ]     #While is acting as a statement modifier.
                                            #It evaluates the operation as long as the condition is truthy.
            ($;=reverse)>$_?$;%$_:$_%$;     #The meat of the program: a ternary operation
            ($;=reverse)                    #The reverse function takes $_ as a parameter by default, and reverses the value.
                                            #The value returned by reverse is stored in the variable $;
                        >$_                 #A condition asking if $% is greater than $_.  Condition of the ternary operation
                           ?$;%$_           #If true, then return $; modulo $_
                                 :$_%$;     #If false, return $_ modulo $;
         $_=                                #Assign the result of the ternary operation back into $_
                                            #If $_ is non-zero, then the condition is true, and while will evaluate the operation
say                                         #Implicitly takes the $_ variable as parameter, and outputs its contents

รหัสดั้งเดิมถูกบันทึกสำหรับลูกหลาน: 43 + 1 = 44 ไบต์

say$_=$%>$_?$%%$_:$_%$%while$_-($%=reverse)

$%>$_?$%%$_:$_%$%คุณเลือก$%ตัวแปรตามจุดประสงค์สำหรับสายนี้หรือไม่?
tomsmeding

เกือบ - ฉันยังบันทึก 1 ไบต์ด้วยการใช้อักขระที่ไม่ใช่ตัวอักษรและตัวเลขสำหรับอักขระตัวสุดท้ายก่อนคำสั่ง while ดังนั้นฉันไม่ต้องการช่องว่าง นอกจากนั้น - สวยมาก
Gabriel Benamy

5

Pyth, 13 12 ไบต์

t.u|%F_S,s_`

ขอบคุณ @TheBikingViking

ลองใช้งานออนไลน์: การสาธิต

รหัสเก่าของฉัน:

W
W=Q%F_S,s_`

ลองใช้งานออนไลน์: การสาธิต

คำอธิบาย:

t.u|%F_S,s_`NNNQ  implicit Ns and Q at the end
               Q  start with N = Q (Q = input number)
        ,         create a pair with the numbers
         s_`N        convert N to string -> reverse-> convert to int
             N       and N
       S          sort
      _           reverse
    %F            fold by modulo
   |          N   or N (if the result is zero use N instead to stop)
 .u               apply this ^ procedure until a value repeats
                  print all intermediate values
 t                except the first one (the original number)

12 t.u|%F_S,s_<backtick>ไบต์: การทดสอบ
TheBikingViking

1
@ TheBikingViking ขอบคุณที่ฉลาดจริงๆ
Jakube

4

เยลลี่ , 15 14 13 ไบต์

,ṚḌṢṚ%/
ÇÇпḊ

TryItOnline

อย่างไร?

,ṚḌṢṚ%/ - Link 1, iterative procedure: n
,       - pair n with
 Ṛ      - reverse n
  Ḍ     - undecimal (int of digit list)
   Ṣ    - sort
    Ṛ   - reverse
     %/ - reduce with mod

ÇÇпḊ - Main link: n
  п  - collect while
 Ç    - last link as a monad is truthy
Ç     -     last link as a monad
    Ḋ - dequeue (remove the input from the head of the resulting list)

4

เยลลี่ , 13 12 ไบต์

,ṚḌṢṚ%/Ṅß$Ṡ¡

นี่คือลิงค์ / ฟังก์ชั่น monadic ที่พิมพ์ไปยัง STDOUT

ลองออนไลน์!

มันทำงานอย่างไร

,ṚḌṢṚ%/Ṅß$Ṡ¡  Monadic link. Argument: n

,Ṛ            Pair n and its reversed digit list.
  Ḍ           Convert the digit list into an integer.
   ṢṚ         Sort and reverse.
     %/       Reduce by modulo. Result: m
          Ṡ¡  Do sign(m) times:
       Ṅß$    Print with a newline and call the link recursively.

ส่วนท้ายของอะไร หากลบรหัสดูเหมือนว่าจะส่งออกต่อท้าย 0
Luis Mendo

ถูกต้อง. 0เป็นค่าตอบแทนของฟังก์ชั่นซึ่งล่ามพิมพ์หากยังไม่ได้ทิ้ง ตามการสนทนาเมตานี้อนุญาตให้ทำได้
Dennis

4

Python 2, 92 87 81 73 61 ไบต์

โซลูชันแบบเรียกซ้ำ:

def f(n):
    r=int(`n`[::-1]);x=min(r%n,n%r)
    if x:print x;f(x)

ลองออนไลน์

โซลูชันที่เกิดซ้ำ: (เช่น61 ไบต์ )

n=input()
while n:r=int(`n`[::-1]);n=min(r%n,n%r);print n/n*n

ลองออนไลน์


วิธีแก้ปัญหาซ้ำ ๆ ที่ฉันให้คุณจริง ๆ แล้วคือ 59 ไบต์ แต่ฉันไม่แน่ใจว่ามันถูกต้องหรือไม่เพราะมันพิมพ์อินพุต หากเป็นเช่นนั้นแล้วคุณสามารถกอล์ฟ 2 while n:ไบต์โดยเพียงแค่การทำ มิฉะนั้นคุณสามารถทำมันได้ด้วย61 ไบต์
FlipTack

3

MATL , 16 ไบต์

`tVPUhSPZ}\tt]xx

ลองออนไลน์!

คำอธิบาย

`         % Do...while
  t       %   Duplicate. Takes input implicitly in the first iteration
  VPU     %   Transform the number at the top of the stack by reversing its digits
  hSPZ}   %   Concatenate the two numbers into an array, sort, reverse, split the
          %   array: this moves the smaller number to the top
  \       %   Modulo
  t       %   Duplicate. The original copy is left on the stack for displaying, 
          %   and the duplicate will be used for computing the next number
  t       %   Duplicate. This copy will be used as loop condition: exit if 0
]         % End
xx        % Delete the two zeros at the top. Implicitly display rest of the stack


2

แบตช์ 140 ไบต์

@echo off
set/pn=
:l
set/am=n,l=0
:r
set/al=l*10+m%%10,m/=10
if %m% gtr 0 goto r
set/an=l%%n%%l+n%%l%%n
if %n% gtr 0 echo %n%&goto l

รับอินพุตบน STDIN และเอาต์พุตลำดับบนบรรทัดแยกกัน รุ่นที่มีคำสั่งตามเงื่อนไข (ซึ่งค่อนข้าง verbose) แต่ไม่มีการแสดงออกตามเงื่อนไขดังนั้นมันง่ายขึ้น (แม้จะมีการอ้างถึง%s) เพื่อคำนวณr%n%r(ซึ่งเท่ากับr%nถ้าn<rหรือศูนย์ถ้าn>r) และn%r%n(ซึ่งเท่ากับn%rถ้าn>rหรือศูนย์ถ้าn<r) และเพิ่ม พวกเขาเข้าด้วยกัน


2

Mathematica, 68 ไบต์

ขอบคุณGreg Martin ที่แนะนำให้ฉันใช้FixedPointListแทนNestWhileList:

FixedPointList[Mod[(r=IntegerReverse@#)~Max~#,r~Min~#]&,#][[2;;-4]]&

ที่สั้นที่สุดที่ฉันจะได้รับโซลูชันดั้งเดิมของฉันFixedPointListคือ 73 ไบต์:

NestWhileList[Mod[(r=IntegerReverse@#)~Max~#,r~Min~#]&,#,#!=0&][[2;;-2]]&

1
โปรดทราบว่าคุณไม่มีเงื่อนไขการยกเลิกที่ถูกต้อง (ลองป้อนตัวอย่าง11000) คุณอาจได้รับสิ่งนี้โดยเปลี่ยนไปใช้เทคนิคที่อธิบายไว้ในย่อหน้าสุดท้ายของคุณ แต่ฉันไม่เห็นวิธีการกำจัดRestหรือMostด้วยวิธีนี้ ในทางกลับกันFixedPointList[ Mod[(r = IntegerReverse@#)~Max~#, r~Min~#] &, #][[2 ;; -4]] &มีเพียง 68 ไบต์เมื่อมีการลบช่องว่างออก (มีข้อผิดพลาดสองข้อคือ nbd)
Greg Martin

ฉันเชื่อว่าตัวเองมีช่วงที่ชอบ{a,b,c,d}[[2;;-4]]จะให้ข้อผิดพลาดมากกว่ารายการที่ว่างเปล่า (ฉันอาจใช้เครื่องหมายจุลภาคมากกว่า;;) เรียนรู้บางสิ่ง
ngenisis

คุณสามารถกำจัดธุรกิจทั้งหมด / นาทีสูงสุดด้วยSort:FixedPointList[-Mod@@Sort@-{#,IntegerReverse@#}&,#][[2;;-4]]&
Martin Ender

1

JavaScript, 72 70 ไบต์

f=(s,...o)=>(u=s>(z=[...s+''].reverse().join``)?s%z:z%s)?f(u,...o,u):o

console.log(...[32452345, 12345678, 11000].map(x=>f(x)))
.as-console-wrapper{max-height:100%!important}

แก้ไข:

-2 ไบต์ : ตัวดำเนินการสเปรดรอการต่อสตริง


1

R, 126 117 ไบต์

x=scan();while(x){y=sort(c(x,as.double(paste(rev(el(strsplit(c(x,""),""))),collapse=""))));if(x<-y[2]%%y[1])print(x)}

น่าเศร้าที่การย้อนกลับตัวเลข ( as.double(paste(rev(el(strsplit(c(x,""),""))),collapse="")))) เป็นคำที่ค่อนข้างสวย พักผ่อนได้ง่าย ๆ ใช้sortเพื่อตรวจสอบทางอ้อมซึ่งสูงกว่า

ส่วนที่เหลือนั้นตรงไปตรงมามันวนซ้ำจนกระทั่งx=0และพิมพ์ทุกขั้นตอน


1

C, 87 ไบต์

t;r;f(n){while(t=n){r=0;while(t)r=10*r+t%10,t/=10;n=r>n?r%n:n%r;if(n)printf("%d ",n);}}

tเป็นการชั่วคราวสำหรับการย้อนกลับ วงด้านในเลื่อนr1 หลักไปทางซ้ายและเพิ่มตัวเลขสุดท้ายของtจนกว่าจะหมด เอาต์พุตคือหลังจากการทำซ้ำครั้งแรกและเฉพาะในกรณีที่ไม่เป็นศูนย์เพื่อป้องกันรายการแรกและสุดท้ายที่จะแสดง

Ungolfed และการใช้งาน:

t;r;
f(n){
  while (t = n){
    r = 0;
    while (t)
      r = 10*r + t%10,
      t /= 10; 
    n = r>n ? r%n : n%r;
    if(n)
      printf("%d ",n);
  }
}

0

Mathematica, 64 ไบต์

NestWhileList[#2~If[#<=#2,Mod,#0]~#&[IntegerReverse@#,#]&,#,#>0&]&

โค้ดด้านบนแสดงถึงฟังก์ชั่น pure ซึ่งรับอินพุตเดียวและส่งกลับลำดับ kuznetsovs สิ่งที่สวยงามมาก ๆ เกี่ยวกับ mathematica ก็คือคุณสามารถวางเลเยอร์ลงบนเลเยอร์ของฟังก์ชั่นที่บริสุทธิ์ ... อนุญาตให้ฉันอธิบายรหัส;)

แต่ละคำในลำดับนั้นถูกคำนวณด้วยฟังก์ชันด้านล่างซึ่งรับหนึ่งอินพุตและส่งคืนคำถัดไป

#2~If[#<=#2,Mod,#0]~#&[IntegerReverse@#,#]&

โค้ดIntegerReverse@#เพิ่งสร้าง r ซึ่งเป็นค่าที่กลับกัน รหัส#2~If[#<=#2,Mod,#0]~#&คือฟังก์ชั่นที่รับอินพุตสองอินพุตและใช้การดำเนินการ mod หรือย้อนกลับอินพุตและเรียกใช้อีกครั้ง อีกวิธีในการเขียนคือIf[#<=#2, Mod, #0][#2, #]&หรืออาจเขียนเป็นฟังก์ชันปกติเช่นนี้:k[a_, b_] := If[a <= b, Mod, k][b, a]


0

แร็กเก็ต 180 ไบต์

(let p((n n)(ol'()))(let*((v reverse)(o modulo)
(r(string->number(list->string(v(string->list(number->string n))))))
(m(if(> n r)(o n r)(o r n))))(if(= m 0)(v ol)(p m(cons m ol)))))

Ungolfed:

(define (f n)
  (let loop ((n n)
             (ol '()))
    (let* ((r (string->number
               (list->string
                (reverse
                 (string->list
                  (number->string n))))))
           (m (if (> n r)
                  (modulo n r)
                  (modulo r n))))
      (if (= m 0)
          (reverse ol)
          (loop m (cons m ol))))))

การทดสอบ:

(f 32452345)
(f 12345678)

ouput:

'(21873078 21418578 1907100 9999)
'(1234575 816021 92313 29655 26037 20988 4950 198 99)
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.