आरटीए (रिवर्स-तब-जोड़ें) एक नंबर की जड़


22

रिवर्स-तब-ऐड (आरटीए) अनुक्रम एक अनुक्रम है जो इसके रिवर्स में एक संख्या जोड़कर और परिणाम पर प्रक्रिया को दोहराता है। उदाहरण के लिए,

5+5=1010+01=1111+11=2222+22=44 ...

इस प्रकार, 5 के आरटीए अनुक्रम में 10, 11, 22, 44, 88, 176, आदि शामिल हैं।

आरटीए जड़ एक नंबर के सबसे छोटी संख्या या तो के बराबर है वह यह है कि एन या करने के लिए बढ़ाने देता है n अपने आरटीए अनुक्रम में।nnn

उदाहरण के लिए, 44, आरटीए अनुक्रम 5, 10, 11, 13, 22, 31, आदि में पाया जाता है, इनमें से 5 सबसे छोटा है, और इसलिए आरटीएआरओटी (44) = 5 है।

72 किसी भी संख्या के आरटीए अनुक्रम का हिस्सा नहीं है, और इसलिए इसे अपना आरटीए मूल माना जाता है।

इनपुट एक सीमा में एक सकारात्मक पूर्णांक है जिसे आपकी भाषा स्वाभाविक रूप से संभाल सकती है।

आउटपुट दी गई संख्या का आरटीए रूट है, जैसा कि ऊपर बताया गया है।

परीक्षण के मामलों

Input
Output

44
5

72
72

132
3

143
49

1111
1

999
999

संबंधित OEIS: A067031 । इस क्रम से आउटपुट एक संख्या होगी।

जवाबों:


13

पर्ल 6 , 45 44 बाइट्स

->\a{first {a∈($_,{$_+.flip}...*>a)},1..a}

इसे ऑनलाइन आज़माएं!

स्पष्टीकरण:

->\a{                                    }  # Anonymous code block
->\a     # That takes a number a
     first  # Find the first element
                                     1..a  # In the range 1 to a
           {                       },    # Where
            a       # a is an element of
              (             ...   )  # A sequence defined by
               $_,  # The first element is the number we're checking
                  {$_+.flip}  # Each element is the previous element plus its reverse
                               *>$a  # The last element is larger than a

5
जब भी मैं इसके पार आता हूं, पर्ल 6 दीर्घवृत्त वाक्य-विन्यास अधिक जादुई हो जाता है। यह लैम्ब्डा-आधारित अनुक्रम विनिर्देश एक ऐसा साफ विचार है!
सूंदर -

@ सूंदर, वह वाक्य रचना वास्तव में मुख्य कारणों में से एक था, जिसके कारण मैं पर्ल 6. पर आया था (और क्यों, कुछ समय बाद, यह मेरी सबसे पसंदीदा भाषा बन गई)
रामिलिज़

7

ब्रेजलॉग , २४ 22 बाइट्स

{~{ℕ≤.&≜↔;?+}{|↰₁}|}ᶠ⌋
  • 2 बाइट्स सूंदर को ध्यान में रखते हुए धन्यवाद कि मैंने ए {{ और}}

व्याख्या

                --  f(n):
                --      g(x):
 {              --          h(y):
  ~             --              get z where k(z) = y
   {            --              k(z):
    ℕ≤.         --                  z>=0 and z<=k(z) (constrain so it doesn't keep looking)
    &≜          --                  label input (avoiding infinite stuff)
      ↔;?+      --                  return z+reverse(z)
   }            --
    {           --                  
     |↰₁        --              return z and h(z) (as in returning either)
    }           --                  
  |             --          return h(x) or x (as in returning either)
 }              --
ᶠ               --      get all possible answers for g(n)
  ⌋             --      return smallest of them

विस्की स्पष्टीकरण के लिए खेद है, यह सबसे अच्छा मैं के साथ आ सकता है

इसे ऑनलाइन आज़माएं!


1
{|↰₁}वहाँ का उपयोग सरल लेकिन शानदार है। अच्छा कार्य!
सूंदर -

5

हास्केल , 59 57 बाइट्स

User1472751 के लिए -2 बाइट्स धन्यवाद ( untilसूची-समझ के बजाय एक दूसरे का उपयोग करके head)!

f n=until((n==).until(>=n)((+)<*>read.reverse.show))(+1)1

इसे ऑनलाइन आज़माएं!

व्याख्या

यह Trueकिसी भी आरटीए-रूट के लिए मूल्यांकन करेगा :

(n==) . until (n<=) ((+)<*>read.reverse.show)

यह शब्द (+)<*>read.reverse.showएक गोल्फ संस्करण है

\r-> r + read (reverse $ show r)

जो उल्टा खुद को एक संख्या जोड़ता है।

यह फ़ंक्शन हमारे लक्ष्य से अधिक होने तक untilबार-बार लागू होता है (+)<*>read.reverse.show

इस सब को लपेटकर अभी तक एक और के untilसाथ शुरू करने 1और 1 को जोड़ने के साथ (+1)पहले आरटीए-रूट मिलेगा।

यदि कोई उचित आरटीए-रूट नहीं है n, तो हम अंततः उस स्थान पर पहुंच जाते हैं nजहां untilसे फ़ंक्शन लागू नहीं होता है n<=n


1
आप untilबाहरी लूप के लिए उपयोग करके 2 बाइट्स बचा सकते हैं : TIO
user1472751

5

05AB1E , 7 बाइट्स

05AB1E के नए संस्करण का उपयोग करना (अमृत में फिर से लिखा गया)।

कोड

L.ΔλjÂ+

इसे ऑनलाइन आज़माएं!

व्याख्या

L           # Create the list [1, ..., input]
 .Δ         # Iterate over each value and return the first value that returns a truthy value for:
   λ        #   Where the base case is the current value, compute the following sequence:
     Â+     #   Pop a(n - 1) and bifurcate (duplicate and reverse duplicate) and sum them up.
            #   This gives us: a(0) = value, a(n) = a(n - 1) + reversed(a(n - 1))
    j       #   A λ-generator with the 'j' flag, which pops a value (in this case the input)
            #   and check whether the value exists in the sequence. Since these sequences will be 
            #   infinitely long, this will only work strictly non-decreasing lists.

रुको .. jएक पुनरावर्ती वातावरण में एक विशेष अर्थ है? मैं केवल पुनरावर्ती वातावरण के माध्यम से और λखुद के बारे में जानता था । क्या इसके अलावा कोई और है j? संपादित करें: आह, मैं कुछ के बारे £में स्रोत कोड में भी देखता हूं । इसका उपयोग कहां के लिए किया जाता है?
केविन क्रूज़सेन

1
@KevinCruijssen हाँ, ये पुनरावर्ती वातावरण में उपयोग किए जाने वाले झंडे हैं। jअनिवार्य रूप से जांचता है कि इनपुट मूल्य अनुक्रम में है या नहीं। £यह सुनिश्चित करता है कि यह अनुक्रम का पहला n मान लौटाता है (जैसा कि λ<...>}¹£)।
अदनान

3

जेली , 12 11 बाइट्स

ṚḌ+ƊС€œi¹Ḣ

9991111

1 बाइट को बंद करने के लिए @JonathanAllan को धन्यवाद!

इसे ऑनलाइन आज़माएं!

यह काम किस प्रकार करता है

ṚḌ+ƊС€œi¹Ḣ  Main link. Argument: n

      €      Map the link to the left over [1, ..., n].
    С         For each k, call the link to the left n times. Return the array of k
               and the link's n return values.
   Ɗ           Combine the three links to the left into a monadic link. Argument: j
Ṛ                Promote j to its digit array and reverse it.
 Ḍ               Undecimal; convert the resulting digit array to integer.
  +              Add the result to j.
       œi¹   Find the first multindimensional index of n.
          Ḣ  Head; extract the first coordinate.

3

रूबी, 66 57 बाइट्स

f=->n{(1..n).map{|m|m+(m.digits*'').to_i==n ?f[m]:n}.min}

इसे ऑनलाइन आज़माएं!

पुनरावर्ती फ़ंक्शन जो बार-बार उस नंबर तक पहुंचने तक आरटीए ऑपरेशन को "अनडू" करता है जो कि इसके द्वारा उत्पादित नहीं किया जा सकता है, फिर न्यूनतम लौटाता है।

उपयोग करने के बजाय filter, जो लंबा है, मैं इसके बजाय केवल map1 से संख्या तक की सीमा पर हूं । इस सीमा में प्रत्येक m के लिए , यदि m + Rev (m) संख्या है, तो यह m पर पुनरावर्ती रूप से फ़ंक्शन को कॉल करता है ; अन्यथा, यह n लौटता है । यह दोनों a की आवश्यकता को दूर करता है filterऔर हमें f (n) = n का बेस केस देता है मुफ्त ।

हाइलाइट में एक बाइट को सहेजना शामिल है Integer#digits:

m.to_s.reverse.to_i
(m.digits*'').to_i
eval(m.digits*'')

अंतिम एक बाइट छोटी होगी, लेकिन दुख की बात है कि रूबी ने 0ऑक्टल के रूप में शुरुआत की ।



2

पायथ , 12 बाइट्स

fqQ.W<HQ+s_`

एक परीक्षण सूट की जाँच करें!

आश्चर्यजनक रूप से तेज और कुशल। सभी परीक्षण मामले एक बार में 2 सेकंड से कम समय तक चले।

यह काम किस प्रकार करता है

fqQ.W<HQ+s_` – Full program. Q is the variable that represents the input.
f            – Find the first positive integer T that satisfies a function.
   .W        – Functional while. This is an operator that takes two functions A(H)
               and B(Z) and while A(H) is truthy, H = B(Z). Initial value T.
     <HQ     – First function, A(H) – Condition: H is strictly less than Q.
        +s_` – Second function, B(Z) – Modifier.
         s_` – Reverse the string representation of Z and treat it as an integer.
        +    – Add it to Z.
             – It should be noted that .W, functional while, returns the ending
               value only. In other words ".W<HQ+s_`" can be interpreted as
               "Starting with T, while the current value is less than Q, add it
               to its reverse, and yield the final value after the loop ends".
 qQ          – Check if the result equals Q.

2

05AB1E , 13 बाइट्स

LʒIFDÂ+})Iå}н

इसे ऑनलाइन आज़माएं!

व्याख्या

L               # push range [1 ... input]
 ʒ         }    # filter, keep elements that are true under:
  IF   }        # input times do:
    D           # duplicate
     Â+         # add current number and its reverse
        )       # wrap in a list
         Iå     # check if input is in the list
            н   # get the first (smallest) one

होशियार! मुझे पता है कि मेरा 21 बाइट्स संस्करण पहले से ही बहुत लंबा था (जो मैंने एक ही दृष्टिकोण के साथ 16 तक गोल्फ किया है), लेकिन वास्तव में इसे कम करने का एक तरीका नहीं निकला। विश्वास नहीं कर सकता कि मैंने फिल्टर के बाद सिर का उपयोग करने के बारे में नहीं सोचा है .. मैं लूप इंडेक्स + 1, या global_counter..> का उपयोग करने की कोशिश करता रहा ।>
केविन क्रूज़सेन

2

जावास्क्रिप्ट (ईएस 6), 61 बाइट्स

n=>(g=k=>k-n?g(k>n?++x:+[...k+''].reverse().join``+k):x)(x=1)

इसे ऑनलाइन आज़माएं!

टिप्पणी की गई

n =>                        // n = input
  (g = k =>                 // g() = recursive function taking k = current value
    k - n ?                 //   if k is not equal to n:
      g(                    //     do a recursive call:
        k > n ?             //       if k is greater than n:
          ++x               //         increment the RTA root x and restart from there
        :                   //       else (k is less than n):
          +[...k + '']      //         split k into a list of digit characters
          .reverse().join`` //         reverse, join and coerce it back to an integer
          + k               //         add k
      )                     //     end of recursive call
    :                       //   else (k = n):
      x                     //     success: return the RTA root
  )(x = 1)                  // initial call to g() with k = x = 1

2

05AB1E , 21 16 15 बाइट्स

G¼N¹FÂ+йQi¾q]¹

-1 बाइट @ @ मिग्ना के लिए धन्यवाद ।

इसे ऑनलाइन आज़माएं।

स्पष्टीकरण:

G               # Loop `N` in the range [1, input):
 ¼              #  Increase the global_counter by 1 first every iteration (0 by default)
 N              #  Push `N` to the stack as starting value for the inner-loop
  ¹F            #  Inner loop an input amount of times
    Â           #   Bifurcate (short for Duplicate & Reverse) the current value
                #    i.e. 10 → 10 and '01'
     +          #   Add them together
                #    i.e. 10 and '01' → 11
      Ð         #   Triplicate that value
                #   (one for the check below; one for the next iteration)
       ¹Qi      #   If it's equal to the input:
          ¾     #    Push the global_counter
           q    #    And terminate the program
                #    (after which the global_counter is implicitly printed to STDOUT)
]               # After all loops, if nothing was output yet:
 ¹              # Output the input

अंतर्निहित मुद्रण के कारण आपको प्रिंट की आवश्यकता नहीं है।
एमिगा

1

चारकोल , 33 बाइट्स

Nθ≔⊗θηW›ηθ«≔L⊞OυωηW‹ηθ≧⁺I⮌Iηη»ILυ

इसे ऑनलाइन आज़माएं! लिंक कोड के वर्बोज़ संस्करण के लिए है। स्पष्टीकरण:

Nθ

इनपुट क्ष

≔⊗θη

असाइन 2क्ष सेवा मेरे ताकि लूप शुरू हो जाए।

W›ηθ«

जबकि दोहराएं >क्ष:

≔L⊞Oυωη

एक डमी नल स्ट्रिंग को धक्का यू इस प्रकार इसकी लंबाई बढ़ रही है, और परिणामी लंबाई को असाइन करें ;

W‹ηθ

दोहराते हुए <क्ष:

≧⁺I⮌Iηη

के रिवर्स जोड़ें सेवा मेरे

»ILυ

की अंतिम लंबाई प्रिंट करें यू जो वांछित जड़ है।


1

MATL , 17 बाइट्स

`@G:"ttVPU+]vG-}@

इसे ऑनलाइन आज़माएं!

व्याख्या

`         % Do...while loop
  @       %   Push iteration index, k (starting at 1)
  G:"     %   Do as many times as the input
    tt    %     Duplicate twice
    VPU   %     To string, reverse, to number
    +     %     Add
  ]       %   End
  v       %   Concatenate all stack into a column vector. This vector contains
          %   a sufficient number of terms of k's RTA sequence
  G-      %   Subtract input. This is used as loop condition, which is falsy
          %   if some entry is zero, indicating that we have found the input
          %   in k's RTA sequence
}         % Finally (execute on loop exit)
  @       %   Push current k
          % End (implicit). Display (implicit)

1
एक साइड नोट के रूप में, मैंने इस 31 बाइट संस्करण का उपयोग करते हुए, टेस्ट केस आउटपुट उत्पन्न करने के लिए MATL का उपयोग किया: :!`tG=~yV2&PU*+tG>~*tXzG=A~]f1) इसे ऑनलाइन आज़माएं!
सूंदर -

1

जावा 8, 103 बाइट्स

n->{for(int i=0,j;;)for(j=++i;j<=n;j+=n.valueOf(new StringBuffer(j+"").reverse()+""))if(n==j)return i;}

इसे ऑनलाइन आज़माएं।

स्पष्टीकरण:

n->{                // Method with Integer as both parameter and return-type
  for(int i=0,j;;)  //  Infinite loop `i`, starting at 0
    for(j=++i;      //  Increase `i` by 1 first, and then set `j` to this new `i`
        j<=n        //  Inner loop as long as `j` is smaller than or equal to the input
        ;           //    After every iteration:
         j+=        //     Increase `j` by:
            n.valueOf(new StringBuffer(j+"").reverse()+""))
                    //     `j` reversed
     if(n==j)       //   If the input and `j` are equal:
       return i;}   //    Return `i` as result

अंकगणितीय रूप से पूर्णांक को उल्टा करना 1 बाइट लंबा ( 104 बाइट्स ) है:

n->{for(int i=0,j,t,r;;)for(j=++i;j<=n;){for(t=j,r=0;t>0;t/=10)r=r*10+t%10;if((j+=r)==n|i==n)return i;}}

इसे ऑनलाइन आज़माएं।


1

सी (जीसीसी) , 120 100 99 बाइट्स

f(i,o,a,b,c,d){for(a=o=i;b=a;o=i/b?a:o,a--)for(;b<i;b+=c)for(c=0,d=b;d;d/=10)c=c*10+d%10;return o;}

इसे ऑनलाइन आज़माएं!

इनपुट को देखते हुए i, प्रत्येक पूर्णांक iको 0 से अनुक्रम की युक्तियों के लिए जाँचता है i

  • i इनपुट मूल्य है
  • o आउटपुट वैल्यू है (अब तक मिली न्यूनतम रूट)
  • a वर्तमान पूर्णांक की जाँच की जा रही है
  • b का वर्तमान तत्व है a अनुक्रम
  • cऔर इसके रिवर्स में dजोड़ने के bलिए उपयोग किया जाता है

के साथ संकलित करने से -DL=forआप 2 बाइट बचा सकते हैं।

उसको खरोंचो; गणित गलत कर रहा है।

हालाँकि, i=o;यदि आप उपयोग करते हैं -O0, तो आप 5 बाइट की बचत करके आउटपुट वैल्यू वापस कर सकते हैं ।

1

जाप , 16 15 11 बाइट्स

@ÇX±swÃøU}a

कोशिश करो

@ÇX±swÃøU}a     :Implicit input of integer U
@        }a     :Loop over the positive integers as X & output the first that returns true
 Ç              :  Map the range [0,U)
  X±            :    Increment X by
    sw          :    Its reverse
      Ã         :  End map
       øU       :  Contains U?


0

सी (जीसीसी) , 89 बाइट्स

मैं प्रत्येक अनुक्रम को [1, n ) में चलाता हूं जब तक कि मुझे एक मैच नहीं मिलता; शून्य विशेष आवरण है क्योंकि यह समाप्त नहीं होता है।

j,k,l,m;r(i){for(j=k=0;k-i&&++j<i;)for(k=j;k<i;k+=m)for(l=k,m=0;l;l/=10)m=m*10+l%10;j=j;}

इसे ऑनलाइन आज़माएं!

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.