誰もGAU番号を知らない


22

GAU番号を紹介します

GAU(1) = 1  
GAU(2) = 1122  
GAU(3) = 1122122333  
GAU(4) = 11221223331223334444  
GAU(6) = 11221223331223334444122333444455555122333444455555666666  
...  
GAU(10) = 11221223331223334444122333444455555122333444455555666666122333444455555666666777777712233344445555566666677777778888888812233344445555566666677777778888888899999999912233344445555566666677777778888888899999999910101010101010101010  

このチャレンジはとても簡単です!

整数n> 0が与えられた場合、GAU(n)の桁数を見つけます

レッツ・メイクGAU(4)
私たちは(私たちは4に到達するまで)は、次の手順を取り、それらを連結

[1][122][122333][1223334444]   

すべての数字をその値と同じ回数だけ記述する必要がありますが、1から毎回カウントする必要があります

1から1までカウントする必要があるGAU(5)
を作成してみましょう

[1]   

次に1から2(ただし、すべての数値をその値と同じ回数だけ繰り返します

[122]     

その後1から3

[122333]   

その後1から4

[1223334444]    

そして最後に1から5まで(GAU(5)を見つけたいのでこれが最後のステップです)

[122333444455555]     

これらすべての手順を実行し、それらを連結すると
、結果はGAU(5)になります

11221223331223334444122333444455555     

これらのGAU番号の桁数に関心があります。

テストケース

入力⟼出力

n   ⟼ Length(GAU(n))

1   ⟼ 1  
2   ⟼ 4  
3   ⟼ 10  
10  ⟼ 230   
50  ⟼ 42190  
100 ⟼ 339240  
150 ⟼ 1295790  

これは挑戦です。
バイト単位の最短コードが勝ちます。

まだ質問がある場合はお知らせください。
ここにいるすべての人に、この魔法のような隠れた複雑なパターンを理解してもらいたい


4
GAUは何の略ですか?
リーキー修道女

21
GはGAU、AとUのために理由もなくただそこにあるある

2
n = 9までは、長さは四面体の数ですが、それを超えると、複数桁の数は単純な閉じた形の邪魔になります
ミフ

参考までに、テストケースにはn ⟼ Length(GUA(n))、GAU(n)ではなくが記載されています。
numbermaniac

2
@numbermaniacこれを見つけてくれてありがとう。GUA番号はまったく異なります。それらはまだ発明されていません!

回答:


14

SOGL V0.1211 10 8 7 5 バイト

∫∫l*+

ここで試してみてください!-これは、スタック上の入力と入力ボックスが空の関数として呼び出されることを想定しています。
入力ボックスから入力を取得する7バイトの代替:

0.∫∫l*+

ここで試してみてください!

0      push 0
 .     push the input
  ∫    iterate over a range 1..POP (input) inclusive, pusing the current number
   ∫    iterate over 1..POP (above loops number) inclusive, pusing the current number
    l    push that numbers length without popping the number
     *   multiply the length by the number
      +  add to the zero, or whatever it is now

push that numbers length without popping the number素敵な
エリックアウトゴルファー


7

Brain-Flak、166バイト

<>(((()()())({}){})())<>{({}[()]<({}({}<<>({}[()])((){[()](<()>)}{}){{}((((({})({})){}{}){}))<>(({}<({}())>){()<({}[({})])>}{})(<>)}{}<>>({}({})())))>)}{}({}<{}{}{}>)

オンラインでお試しください!

説明

<>(((()()())({}){})())<>           # Initialize second stack with 9 and 10
{({}[()]<                          # Do main loop n times:
  ({}
    ({}
      <
        <>({}[()])                 # Subtract 1 from counter to next power of 10
        ((){[()](<()>)}{}){        # If reached a power of 10 (say, 10^k):
          {}((((({})({})){}{}){})) # Multiply existing (10^k*0.9) by 10 and push twice
          <>                       # On first stack
          (
            ({}<({}())>)           # Increment length of numbers
            {()<({}[({})])>}{}     # Divide length of new set of numbers by this length
          )                        # Add together to get new set of numbers length
        (<>)}  
      {}<>>  
      ({}({})())                   # Add number length to number set length
    )                              # Add number set length to new segment length
  )                                # Add new segment length to total length
>)}                                # End main loop
{}({}<{}{}{}>)                     # Put result on stack by itself





3

、7バイト

Σ∫mS*Lḣ

オンラインでお試しください!

非ゴルフ/説明

         -- implicit input N                        | 10
  m   ḣ  -- map the following function over [1..N]  | [1,2,3,4]
   S*L   --   multiply length of number by itself   | [1,2,3,4] (only important for numbers ≥ 10)
 ∫       -- prefix sums                             | [0,1,3,6,10]
Σ        -- sum                                     | 20

3

、7バイト

ṁLṁṘNḣḣ

オンラインでお試しください!

説明

          Implicit input, e.g 4
      ḣ   Range from 1 to n                               [1,2,3,4]
     ḣ    Prefixes                                        [[],[1],[1,2],[1,2,3],[1,2,3,4]]
  ṁ       Map and then concatenate
   ṘN     Repeat each number in each list by its index    [[],[1],[1,2,2],[1,2,2,3,3,3],[1,2,2,3,3,3,4,4,4,4]]
                                                          [1,1,2,2,1,2,2,3,3,3,1,2,2,3,3,3,4,4,4,4]
ṁ         Map and then sum
 L        Length (of number: 10 -> 2)                     26

ああ、もう一つのハスクの解決策:)同じバイト数で私のものを投稿したときにあなたの提出物は見られませんでしたが、それらは十分に異なるので、私もここに残します。
ბიმო



3

CJam、20バイト

q~),(\{),{_s,*+}*+}%

オンラインでお試しください!

番号は「入力」フィールドに渡されます。

ゴルフのない説明:(入力例= 2)

q~),(\{),{_s,*+}*+}%                                             | Stack:
q                     read input as string                       | "2"
 ~                    eval input (add it to stack as integer)    | 2
  )                   add 1                                      | 3
   ,                  range (convert to array with values 0...N) | [0, 1, 2]
    (                 pop first item of array                    | [1, 2] 0
     \                swap top two values of stack               | 0 [1, 2]
      {           }   for each item in array...                  | 0 1
       )              add 1                                      | 0 2
        ,             range (convert to array with values 0...N) | 0 [0, 1]
         {     }      for every element in the array...          | 0 0
          _           duplicate                                  | 0 0 0
           s          convert to string                          | 0 0 "0"
            ,         get length of string                       | 0 0 1
             *        multiply                                   | 0 0
              +       add                                        | 0 1
                *     fold                                       | 0 1
                 +    add                                        | 1
                   %  repeat                                     | 4

笑説明すると難しいようです。


2

J、24バイト

[:+/[:+/\[:(*#@":"0)1+i.

dzaimaのAPL回答に対する同様の高レベルのアプローチはJに変換されますが、ログの代わりに最初に文字列に変換することで数値の長さを計算し、Jのフックを使用してその長さを数値自体で乗算します(*#@":"0)。その後は、スキャン合計の合計になります。

オンラインでお試しください!


1
1(#.]*#\*#\.)1#@":@+i.22バイトでも動作します
マイル

@milesそれは賢い-それを理解するのに少し時間がかかった。Jでのプログラミングはどれくらいですか?
ジョナ

コードゴルフに参加してから少し。私はそれを読むことができるとは知らないので、実際のプログラムを書くために実際には使用しませんが、現在は高度なデスクトップ計算機として使用しており、通常は常に何かを計算するためのウィンドウを開いています。
マイル

2

R、39バイト

function(n)sum(nchar(rep(1:n,n:1*1:n)))

すべてのテストケースを検証してください!

シンプルなアルゴリズム。私は、ほとんどの人がそうであったように、iinの1:n場合iは繰り返されることを観察しましたi*(n-i+1)。そこで、私はそのベクトルを作成し、それぞれの文字数をカウントし、それらを合計します。


1

パイソン2、51の 50バイト

lambda n:sum(~k*(k-n)*len(`k+1`)for k in range(n))

@LeakyNunなぜ?私はこの答えを自分で開発しました。他の答えもチェックしませんでした。
orlp

1
これは正しい答えさえ出力せず、n = 1の場合は0、n = 2の場合は3、n = 3の場合は14を与えます
Halvard Hummel

@HalvardHummelおっと、サインを台無しにして+1を忘れました。修正されました。
orlp

ようやくパターンが理解できたと思います!コードをオンラインでテストする方法はありますか?他のPython 2の回答でもこれをカバーしていますか?

1

JavaScript(ES6)、50 42バイト

更新:現在、基本的に他の回答が行っていることの移植版です。

f=(n,i=1)=>n&&`${n}`.length*n*i+f(n-1,i+1)

テストケース


1

Mathematica、66バイト

Tr[1^(f=Flatten)[IntegerDigits/@f@(a=Array)[a[#~Table~#&,#]&,#]]]&



1

Japt12 11 10 9バイト

õõÈ*sÊÃxx

試すか1〜150のすべての数値をテストしてください


説明

integerの暗黙的な入力U

õõ

1〜の整数の配列を生成し、U1〜各整数のサブ配列を生成します。

È   Ã

関数を介して各サブ配列の要素を渡します。

*sÊ

現在の要素を文字列(s)に変換し、長さ(Ê)を取得して、要素で乗算します。

xx

最初に各サブアレイに同じことを行った後、メインアレイを追加して減らします。


1

JQ 1.582の 49 43バイト

[range(.)+1|range(.)+1|"\(.)"*.|length]|add

拡大

[   range(.)+1        # for i=1 to N
  | range(.)+1        # for j=1 to i
  | "\(.)"*.          # "j" copied j times
  | length            # convert to length
] | add               # add lengths

サンプル実行

$ jq -Mr '[range(.)+1|range(.)+1|"\(.)"*.|length]|add' <<< "150"
1295790

オンラインでお試しください!またjqplay.org


1

積み上げ、28バイト

[~>[~>[:rep]"!]"!flat''#`#']

オンラインでお試しください!

「エイリアスはどの時点で読めないのですか?」と尋ねる人もいるかもしれません。これが近い場合、「読みやすさ」の非常にリベラルな定義があります。

説明

[~>[~>[:rep]"!]"!flat''#`#']    input: N
 ~>[          ]"!               for each number K from 1 to N
    ~>[    ]"!                  for each number J from 1 to K
       :rep                     repeat J J times
                 flat           flatten the resultant array
                     ''#`       join by the empty string
                         #'     get the length of said string


1

C#(.NET Core)94 80 74バイト

n=>{int b=0,a=0,i;while(a++<n)for(i=0;i++<a;)b+=(i+"").Length*i;return b;}

オンラインでお試しください!

@ kamoroso94の答えが始まったような直接的な解決策を見つけたいと思っていましたが、あまりにも多くの時間を費やしていたのでgaveめました。おそらくそれを行う方法はありますが、式はすべての等級ステップに合わせて調整する必要があります。

謝辞

@someoneのおかげで14バイト節約

@Kevin Cruijssenのおかげで6バイト節約


1
n=>{int b=0,a=0,i;for(;a++<n;)for(i=0;i++<a;)b+=i.ToString().Length*i;return b;} オンラインでお試しください!80バイトとパフォーマンスのため。
私の代名詞はモニカレインステート

1
i.ToString()(i+"")さらにバイトを節約することができます。
ケビンCruijssen


1

Perl 6、36バイト

{[+] 1..*Z*($_...1).map:{.chars*$_}}

試して

拡張:

{  # bare block lambda with implicit parameter 「$_」

  [+]               # reduce the following using &infix:«+»

    1 .. *          # Range from 1 to infinity

    Z*              # zip using &infix:«*»

    ( $_ ... 1 )    # sequence from the input down to 1
    .map:           # for each one
    { .chars * $_ } # multiply the number of digits with itself
}

1

18 14バイト

IΣE⊕NΣE⊕ι×λLIλ

オンラインでお試しください!リンクは、コードの詳細バージョンです。編集:を使用しSumて4バイトを保存しました。説明:

  E⊕N           Map from 0 to the input (loop variable i)
      E⊕ι       Map from 0 to i (loop variable l)
            Iλ  Cast l to string
           L    Take the length
         ×λ     Multiply by l
     Σ          Sum the results
 Σ              Sum the results
I               Cast to string
                Implicitly print

:| 文字列引数が与えられたときの合計は、文字列内の数値を合計
ASCIIのみ

@ASCIIのみそれはそうではなく、Σ代わりに印刷するだけ
ニール

@ ASCIIのみ。また、私ができる最善のはSum、まだ18バイトである:Print(Cast(Sum(Map(InclusiveRange(1, InputNumber()), Sum(Map(InclusiveRange(1, i), Times(l, Length(Cast(l)))))))));
ニール・


@ASCIIのみ製品の合計を試しましたが、17バイトでした:≔⊕NθIΣEθ×⁻θι×ιLIι。ただし、以前のコメントから4バイト削るIncremented代わりに使用しInclusiveRangeます。
ニール


1

[Dyalog APL]、22 20バイト

{+/≢¨⍕¨↑,/(/⍨¨⍳¨⍳⍵)}

オンラインでお試しください!

説明:

{+/≢¨⍕¨↑,/(/⍨¨⍳¨⍳⍵)}
{                  } anonymous function with right argument named 
                ⍳⍵   range 1 to right arg
              ⍳¨     for each, range 1 to it
             ¨       for each
           /⍨          for each item, repeat right arg left arg times
          (       )  take that and
        ,/           join the sub-arrays together
                    convert from a nested array to a simple array (or something like that, I don't quite understand it :p)
     ⍕¨              convert each number to a char-array (aka string version)
   ≢¨                get length of each
 +/                  sum that together

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.