Sixers dizisindeki ilk oluşum


17

Sixers sekans dizisi verilebilir bir isimdir A087409 . Bu sıra hakkında bir Numberphile video öğrendim ve aşağıdaki gibi inşa edilebilir:

İlk olarak, taban 10'da yazılı 6'nın katlarını alın:

6, 12, 18, 24, 30, 36, ...

Ardından, sayıları bir basamak akışına birleştirin:

61218243036...

Son olarak, akışı çiftler halinde yeniden gruplandırın ve her birini bir tamsayı olarak yorumlayın:

61, 21, 82, 43, 3, ...

Sayıları çiftler halinde gruplandırdığımızda, sekanstaki maksimum sayı 99 olur ve sekansta 100'den küçük negatif olmayan tamsayıların temsil edildiği ortaya çıkar. Bu zorluk, Sixers dizisindeki bir sayının ilk örneğinin dizinini bulmaktır.

Giriş

Aralıktaki bir tam sayı [0-99]. Bu aralığın dışındaki sayıları hesaba katmanıza gerek yoktur ve böyle bir girdi verilirse çözümünüzün herhangi bir davranışı olabilir.

Çıktı

Sixers dizisindeki giriş numarasının ilk tekrarlama dizini. Bu, 0- veya 1 endeksli olabilir; lütfen cevabınızda hangisini kullandığınızı söyleyin.

kurallar

  • Girişte belirtilen diziyi oluşturma prosedürü yalnızca açıklama amaçlıdır, sonuçlar aynı olduğu sürece istediğiniz herhangi bir yöntemi kullanabilirsiniz.
  • Tüm programları veya işlevleri gönderebilirsiniz.
  • Her türlü giriş ve çıkış yöntemine izin verilir.
  • Standart boşluklara izin verilmez.
  • Kodunuzu çevrimiçi test etmek için bağlantılar önerilir!
  • Bu , her dilde en kısa cevap kazanır!

Test senaryoları

Biçimdeki tüm giriş ve çıkışların bir listesi input, 0-indexed output, 1-indexed output.

0   241 242
1   21  22
2   16  17
3   4   5
4   96  97
5   126 127
6   9   10
7   171 172
8   201 202
9   14  15
10  17  18
11  277 278
12  20  21
13  23  24
14  19  20
15  29  30
16  32  33
17  297 298
18  35  36
19  38  39
20  41  42
21  1   2
22  46  47
23  69  70
24  6   7
25  53  54
26  22  23
27  11  12
28  62  63
29  219 220
30  65  66
31  68  69
32  71  72
33  74  75
34  49  50
35  357 358
36  80  81
37  83  84
38  25  26
39  89  90
40  92  93
41  27  28
42  42  43
43  3   4
44  101 102
45  104 105
46  8   9
47  177 178
48  110 111
49  13  14
50  28  29
51  119 120
52  122 123
53  417 418
54  79  80
55  128 129
56  131 132
57  134 135
58  55  56
59  437 438
60  140 141
61  0   1
62  31  32
63  75  76
64  5   6
65  120 121
66  82  83
67  10  11
68  161 162
69  164 165
70  58  59
71  477 478
72  170 171
73  173 174
74  34  35
75  179 180
76  182 183
77  497 498
78  85  86
79  188 189
80  191 192
81  18  19
82  2   3
83  78  79
84  93  94
85  7   8
86  37  38
87  168 169
88  12  13
89  228 229
90  88  89
91  218 219
92  221 222
93  224 225
94  64  65
95  557 558
96  230 231
97  233 234
98  40  41
99  239 240

6
6, 2*6, 3*6,..., 325*6
Düşünmenin

@LuisMendo Haklısın, bunu meydan okuma açıklamasına dahil edip etmeyeceğimi tartışıyordum. Bir yorum da bunun için iyi bir yer: o)
Sok

Girdi-tamsayıyı n < 10 olanlarla dize olarak alabilir miyizn<10 ile yastıklı bir lider 0 (yani 00, 01, 02, ...)?
Kevin Cruijssen

10
@KevinCruijssen Hmmm, dize olarak giriş iyi, ancak 0 ile sol dolgu biraz fazla IMO.
Sok

Yanıtlar:


12

JavaScript (ES6),  71 65  55 bayt

Çıktı 0 dizinlidir.

n=>(g=([a,b,...c])=>b?a+b-n&&1+g(c):g([a]+6*++i))(i='')

Çevrimiçi deneyin!

Nasıl?

Özyinelemeli bir işlev kullanarak, 6 birleştirilmiş katlar dizesinin ilk 2 karakterini 'tüketiriz' veya 2'den az varsa yeni karakterler ekleriz.

N için örnekn=3:

 string | operation                          | result
--------+------------------------------------+--------
 ''     | not enough characters: append '6'  |   0
 '6'    | not enough characters: append '12' |   0
 '612'  | consume '61', increment the result |   1
 '2'    | not enough characters: append '18' |   1
 '218'  | consume '21', increment the result |   2
 '8'    | not enough characters: append '24' |   2
 '824'  | consume '82', increment the result |   3
 '4'    | not enough characters: append '30' |   3
 '430'  | consume '43', increment the result |   4
 '0'    | not enough characters: append '36' |   4
 '036'  | consume '03': success              |   4

Yorumlananlar

n => (             // n = input
  g = (            // g is a recursive function taking either a string or an array of
                   // characters split into:
    [a, b,         //   a = 1st character, b = 2nd character,
           ...c]   //   c[] = array of all remaining characters
  ) =>             //
    b ?            // if b is defined:
      a + b - n && //   if n subtracted from the concatenation of a and b is not zero:
        1 + g(c)   //     add 1 to the final result and do a recursive call with c[]
                   //   (otherwise: yield 0 and stop recursion)
    :              // else:
      g(           //   do a recursive call with:
        [a] +      //     the concatenation of a (forced to an empty string if undefined)
        6 * ++i    //     and 6 * i, with i pre-incremented
      )            //   end of recursive call
)(i = '')          // initial call to g with an empty string,
                   // and i set to empty string as well (zero'ish)

12

Python 2 , 93 92 85 83 81 68 65 59 bayt

f=lambda n,s='612',i=18:n-int(s[:2])and-~f(n,s[2:]+`i`,i+6)

Çevrimiçi deneyin!


  • -2 bayt, Grimy sayesinde
  • -3 bayt, ArBo sayesinde
  • -6 bayt, xnor sayesinde

1
3 bayt bir lambda olarak daha kısa:f=lambda n,s='612',i=3:n-int(s[:2])and f(n,s[2:]+`i*6`,i+1)or i-2
ArBo

@ArBo Daha da iyisi, f=lambda n,s='612',i=18:n-int(s[:2])and-~f(n,s[2:]+`i`,i+6)(0 dizinli).
xnor

8

Perl 6 , 31 bayt

{+(comb(2,[~] 1..ⅮX*6)...$_)}

Çevrimiçi deneyin!

1 dizinli diziyi kullanır.

Açıklama:

{                            } # Anonymous code block
              1..Ⅾ             # The range 1 to 500
                   X*6         # All multiplied by 6
          [~]                  # Join as one giant string
   comb(2,            )        # Split into pairs of characters
                       ...$_   # Take up to the input
 +(                         )  # And return the length of the list


5

05AB1E , 9 bayt

₄L6*J2ôIk

0 endeksli. Girdi olarak tek bir tamsayı veya bir tamsayı listesi kabul eder.

Çevrimiçi deneyin veya tüm test senaryolarını doğrulayın .

Açıklama:

L         # Create a list in the range [1,1000]
  6*       # Multiply each value by 6
    J      # Join the entire list of integers together to a string
     2ô    # Split into parts of size 2
       Ik  # Get the index of the input integer(s)
           # (and output the result implicitly)

Dize olarak birleştirilecek varsayılan davranış mı yoksa dize olarak katılmak ve sayı olarak katılmak için ayrı işleçler var mı?
maxb

@maxb Genel olarak 05AB1E için herhangi bir açık dönüştürmeye gerek yoktur. Tüm tamsayılar, replace veya split gibi dize işlevleri için de kullanılabilir ve oluşturulan tüm dizeler (tamsayılardır) sayı olarak da kullanılabilir. Yani 100, "100"ve 100.0eşit kontroller gibi birçok işlev için aynıdır. 05AB1E'de sıralama (sayısal veya sözlükbilimsel sıralama) gibi bazı işlevler için veya int'e döküm yaparken bir şamandıradan sonraki ondalık basamakları kaldırmak için hala int'e ve dize işlevlerine döküm var, ancak bunlar sık ​​kullanılmıyor .
Kevin Cruijssen

@maxb Bazı ek örnekler veren ilgili 05AB1E ipucu .
Kevin Cruijssen

4

Kömür , 12 bayt

I⌕I⪪⭆φ×⁶⊕ι²N

Çevrimiçi deneyin! Bağlantı, kodun ayrıntılı versiyonudur. 0 endeksli. Açıklama:

     φ           Predefined constant 1000
    ⭆           Map over implicit range and join
        ι       Current index
       ⊕        Incremented
     ×⁶         Multiplied by 6
   ⪪      ²     Split into pairs of digits
  I             Cast to integer
           N    Input as a number
 ⌕              Find its index
I               Cast to string
                Implicitly print


4

APL (Dyalog Unicode) , 26 bayt

{⍵⍳⍨⍎¨((≠\=⍨)⊂⊢)∊⍕¨6×⍳325}

Çevrimiçi deneyin! - Tüm geçerli girişleri test eder.

Nasıl:

{⍵⍳⍨⍎¨((≠\=⍨)⊂⊢)∊⍕¨6×⍳325}  Dfn, input is ⍵.
                    6×⍳325   Generates the first 325 multiples of 6.
                  ⍕¨         Format each number into a string
                            Enlist, flattens the vector
       (      ⊂⊢)            Dyadic enclose, takes a boolean mask as left argument
        (≠\=⍨)               Generates the mask 1 0 1 0...
                             Enclose then returns the Sixers sequence as a string
     ⍎¨                      Execute each element in the string, turning it into a numeric vector
 ⍵⍳⍨                         Find the first occurrence of  in the vector

Düzleştirilmiş vektörü K'deki gibi yeniden şekillendirebilir misiniz? Google önerdi ancak APL beni korkutuyor ...
streetster

@streetster evet, APL'nin yeniden şekillenmesi. Düzleştirilmiş bir vektörü yeniden şekillendirmek istiyorsanız, yapmanız gereken<new shape vector> ⍴ <vector to reshape>
J. Sallé

2xN listesini oluşturmak ve sonra her birini tamsayıya dönüştürmek için reshape kullanabilirsiniz.
streetster

Yapabilirsin, ama şimdiki cevabımdan daha kısa olacağını sanmıyorum. Bir sorun, cevabım için, dizeyi 1117 × 2 matrisine yeniden şekillendirmek ve daha sonra tamsayılara dönüştürmek, 1117 ile bir basamaklı tamsayılar vektörü oluşturmak olacaktır.
Yeniden

Ah, düzleştirilmiş dize alır yeniden şekillendirmiştir daha :) çalışılabilir bir hale
streetster

3

Python 3, 87 81 Bytes:

lambda n:[*zip(*[iter(''.join(map(str,range(6,1951,6))))]*2)].index((*'%02d'%n,))

integer input, 0-indexed output.

Try it online!


-6 bytes, thanks to @TFeld.





2

MathGolf, 10 bytes

•╒6*y░2/i=

Try it online!

Basically the same as the 05AB1E answer, but I lose a byte by having to convert the concatenated number to string explicitly.

Explanation

•╒             push [1, 2, ..., 512]
  6*           multiply by 6
    y          join array without separator to string or number
     ░         convert to string (implicit map)
      2/       split into groups of 2 characters
        i      convert to integer (implicit map)
         =     find index of implicit input in the array



2

K (oK), 22 bytes

Solution:

(.:'0N 2#,/$6*1+!999)?

Try it online!

Explanation:

0-indexed.

(.:'0N 2#,/$6*1+!999)? / the solution
                     ? / lookup right in left
(                   )  / do this together
                !999   / range 0..999
              1+       / add 1, range 1...1000
            6*         / multiply by 6, 6...6000
           $           / convert to strings
         ,/            / flatten
    0N 2#              / reshape into 2xN
 .:'                   / value each, convert to numbers

2

Jelly, 10 bytes

ȷ×€6DFs2Ḍi

Try it online!

TIO link gives all values for 0 to 99.

Explanation

ȷ          | 1000
 ×€6       | each times 6 (using implicit range from 1..1000)
    D      | Convert to decimal digits
     F     | Flatten
      s2   | Split into pairs
        Ḍ  | Convert back from decimal digits to integer
         i | Find index of left argument to link

2

Java 10, 119 104 102 bytes

n->{int i=2;for(var s="612";!s.substring(0,2).equals(""+n/10+n%10);)s=s.substring(2)+6*++i;return~-i;}

Port of @TFeld's Python 2 answer.
-2 bytes thanks to @Imus.

1-indexed.

Try it online.

Explanation:

n->{                            // Method with integer as both parameter and return-type
  int i=2;                      //  Index-integer, starting at 2
  for(var s="612";              //  String, starting at "612"
      !s.substring(0,2)         //  Loop as long as the first two characters of the String
       .equals(                 //  Are not equal to:
               ""+n/10          //   The input integer-divided by 10 as String
               +n%10);)         //   Concatenated with the input modulo-10
                                //   (which will add leading 0s for inputs < 10)
    s=s.substring(2)            //   Remove the first two characters of the String
      +6*++i;                   //   And append 6 times `i`,
                                //   after we've first increased `i` by 1 with `++i`
return~-i;}                     //  Return `i-1` as result

Original 119 117 bytes version:

n->{var s="";for(int i=0;i<2e3;)s+=i+=6;return java.util.Arrays.asList(s.split("(?<=\\G..)")).indexOf(""+n/10+n%10);}

0-indexed.

Try it online.

Explanation:

n->{                            // Method with integer as both parameter and return-type
  var s="";                     //  String we're building, starting empty
  for(int i=0;i<2e3;)           //  Loop `i` in the range [0, 2000):
      s+=i+=6;                  //   Increase `i` by 6 first every iteration
                                //   And then append the updated `i` to String `s`
  return java.util.Arrays.asList(
          s.split("(?<=\\G..)") //  Split the String in parts of size 2 (as array)
         )                      //  Convert the array to a List
          .indexOf(             //  And get the index of the following in this list:
                   ""+n/10      //   The input integer-divided by 10 as String
                   +n%10);}     //   Concatenated with the input modulo-10

1
You can save 2 bytes by using ""+n/10+n%10 instead of n>9?n+"":"0"+n
Imus

1

CJam, 17 bytes

325,:)6f*s2/:~ri#

Try it online!

0-based.

Explanation

325,   e# Range [0 1 2 ... 324]
:)     e# Add 1 to each: gives [1 2 3 ... 325]
6f*    e# Multiply each by 6: gives [6 12 18 ... 1950]
s      e# Convert to string: gives "61218...1950"
2/     e# Split into chunks of size 2: gives ["61" "21" ... "95" "0"]
       e# Note how the last chunk has size 1; but it is not used
:~     e# Evaluate each string in that array: gives [61 21 ... 95 0]
ri     e# Read input as an integer
#      e# Index of fist occurrence, 0-based

Out of curiosity, why does CJam has builtins for all integers in the range [10,20], as well as five different builtins that all default to an empty string "", but no builtins for 100 or 1000?
Kevin Cruijssen

@KevinCruijssen Not sure... But having variables with predefined values like 0 or "" is sometimes useful for loops, because those are often the desired starting values. As for not having 100 or 1000, yes, I agree they would more useful than say 18 or 19
Luis Mendo

1
It's a shame that leading zeroes are annoying, otherwise you could ditch the :~ and i from your code. :(
Erik the Outgolfer

1

Japt, 12 bytes

0-indexed.

L²õ*6 ¬ò b¥U

Try it or test all inputs

L²õ*6 ¬ò b¥U     :Implicit input of integer U
L                :100
 ²               :Squared
  õ              :Range [1,L²]
   *6            :Multiply each by 6
      ¬          :Join to a string
       ò         :Split to array of strings each of length 2
         b       :First 0-based index of
          ¥U     :Test for equality with U (bU wouldn't work here as each string would first need to be cast to an integer, costing more bytes)




1

Retina, 83 77 bytes

I am really out of practice at complicated programming in Retina, but I'm satisfied with the length I managed to do it in.

Outputs the 0-indexed result.

.+
6*1
325+-1%`1+
$0¶6*1$0
1+
$.0
¶

L`..
m`^0

$
¶$+
s`\b(\d+)\b.*\b\1$

C`¶

Try it Online


Explanation

.+                   Replace the input with 6 in unary
6*1
325+-1%`1+           Do 325 times: append line with previous + 6
$0¶6*1$0
1+                   Convert all lines to decimal
$.0
¶                    Remove line breaks

L`..                 List pairs of digits
m`^0                 Remove leading zeros

$                    Append the original input N on a new line
¶$+
s`\b(\d+)\b.*\b\1$   Remove occurrences of N and anything in between

C`¶                  Count the number of line breaks


1

Retina 0.8.2, 36 bytes

^
2406$*_
_{6}
$.`
^0(..)+?.*\1$
$#1

Try it online! Link includes test suite. 1-indexed. Explanation:

^
2406$*_

Prefix 2406 _s to the input.

_{6}
$.`

Replace every 6 _s with the number of preceding _s. This generates the sequence 0, 6, 12 ... 2400, but automatically concatenates the numbers.

^0(..)+?.*\1$

Skip the leading 0 and find the first pair of digits that match the last two digits i.e. the zero-padded input (because the string ends in 0; in fact the test suite uses the fact that it ends in 00).

$#1

Output the number of pairs of digits up to and including the match.

Retina 1 saves a couple of bytes because its string repetition operator is a byte shorter and already defaults to _ as its right-hand operand, so that the second line of code becomes just 2406*. Another feature of Retina 1 is the > modifier which generates the substitution in the context of the separator after the match, which in the case of $.>` causes it to include the length of the match in the result. Although this costs a byte we save it immediately as we don't need to match the 0 any more. (The repetitions also have to be reduced by 6.) Retina 1 can also do basic arithmetic in a substitution. This means that we don't have to resort to tricks to take multiples of 6, instead we just generate the numbers 1..400 and multiply by 6 in the substitution. Remarkably, this also doesn't affect the overall byte count, as the final result looks like this:

^
400*
_
$.(6*$>`
^(..)+?.*\1$
$#1




1

Clojure, 102 bytes

#(count(for[i(partition 2(for[i(range 1 326)c(str(* i 6))]c)):while(not=(seq(str(if(< % 10)0)%))i)]i))

So long! :(

Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.