ラウンド・ミー、ヘルプ・ミー


23

入力を考えるとn、あなたのプログラムや関数を出力しなければならない整数最小の正kなどnの最も近い倍数に丸めはkより大きくなりますn

例。

入力を指定する20と、出力値は次のようになります3

  • 倍数1IS 20である、ないより大きい20

  • 倍数2IS 20である、ないより大きい20

  • の最も近い倍数は321、これより大きいため20、出力されます。

テストケース

#Input  #Output
2       3
4       5
6       4
8       3
10      4
12      7
14      3
16      6
18      4
20      3
22      4
24      5
26      3
28      5
30      4
32      3
34      4
36      8
38      3
40      6
42      4
44      3
46      4
48      5
50      3
52      6
54      4
56      3
58      4
60      7
62      3
64      5
66      4
68      3
70      4
72      11
74      3
76      6
78      4
80      3
82      4
84      5
86      3
88      5
90      4
92      3
94      4
96      7
98      3
1000    6

奇数の入力を与えられた出力は2でなければなりません。

ルール

  • n より小さい正の整数です 2^32
  • 丸めは、の2つの倍数がkから等しく離れているn場合、大きい方が選択されるように実行されます(「切り上げ」)。このように、すべての奇数nはの出力を生成します2
  • これはなので、各言語で最短のコード優先されます。

読みやすく、簡潔にするために、テストケースの形式を編集しました。これに問題がある場合、または新しい例のいずれかがオフになっている場合はお知らせください。:)
DJMcMayhem

@Shaggy Done!リストから500のオッズと450のイーブンを削除しました。
fireflame241

このシーケンスのoeisリンクはありますか?
ジェームズK

@JamesK以前に検索したときに見つけられませんでした。おそらくOEISアカウントを持っている人が作成できますか?
fireflame241

回答:



9

Japt、6バイト

@<rX}a

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

説明:

@    <r X}a
XYZ{U<UrX}a
X              // X = 0; Increments when the condition in between {...} fails
   {     }a    // Return the first integer X where:
    U          //   The input
     <U        //   is less than the input
       rX      //     rounded to the nearest multiple of X

2
r組み込みですか?o_o
エリック・ザ・アウトゴルファー

@EriktheOutgolfer:Japtには切り上げまたは切り捨てのためのビルトインもあります:)
シャギー

5
私は知っていた、この機能は便利いつか来るでしょう:D
ETHproductions

@シャギーそれはナッツです!o_o_o
エリック

@Oliver:これは、より多くの今、機能の方法でグリップを得るために確信させた私を持っている-これの私自身のバージョンは7つのバイトでした:o æ@<rX
シャギー

7

MATL、13バイト

tQ:yy/Yo*<fX<

オンラインでお試しください!またはから1までのすべての入力を確認し1000ます。

説明

入力を検討してください6

t      % Implicit input. Duplicate
       % STACK: 6, 6
Q:     % Add 1, range
       % STACK: 6, [1 2 3 4 5 6 7]
yy     % Duplicate top two elements
       % STACK: 6, [1 2 3 4 5 6 7], 6, [1 2 3 4 5 6 7]
/      % Divide, element-wise
       % STACK: 6, [1 2 3 4 5 6 7], [6 3 2 1.5 1.2 1 0.8571]
Yo     % Round to closest integer. Halves are rounded up
       % STACK: 6, [1 2 3 4 5 6 7], [6 3 2 2 1 1 1]
*      % Multiply, element-wise
       % STACK: 6, [6 6 6 8 5 6 7]
<      % Less than, element-wise
       % STACK: [0 0 0 1 0 0 1]
f      % Find: indices of nonzeros (1-based)
       % STACK: [4 7]
X<     % Minimum of vector. Implicit display
       % STACK: 4


5

JavaScript(ES6)、28 25バイト

n=>g=x=>n%x>=x/2?x:g(-~x)
  • Arnauldのおかげで3バイト節約されました。

試して

o.innerText=(f=

n=>g=x=>n%x>=x/2?x:g(-~x)

)(i.value=64)();oninput=_=>o.innerText=f(+i.value)()
<input id=i type=number><pre id=o>

または、1〜1000のすべての数値をテストします(実行に1分かかります)。


5

プロトン、33バイト

n=>[x for x:2..n+2if n%x>=x/2][0]

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


プロトンについては何も知りませんが、3バイト節約できるようです:オンラインで試してみてください!
jferard

たぶん偶然、これは... totallyhumanのソリューションとまったく同じである:P
エリックOutgolfer

@EriktheOutgolfer同時に(実際、私は数秒で彼を忍者にした)37バイトでそれを投稿しました。Hyperがオペレーターを中断し、彼がそれらを修正したとき、私たちは両方とも更新しました。
ミスターXcoder

うーん、私はあなたにIIRCを忍び込ませた。:P-
完全に人間

@totallyhumanあなたは41バターで私を忍ばせた。私は最初に37-byterを投稿し、それを数秒で忍者に送りました。
ミスターXcoder



3

ゼリー、11バイト

÷R%1<.¬;1TṂ

正の整数を受け取り、返す単項リンク。

オンラインでお試しください!またはテストスイートを

どうやって?

÷R%1<.¬;1TṂ - Link: number, n       e.g. 10
 R          - range(n)               [ 1,2,3     ,4  ,5,6     ,7     ,8   ,9     ,10]
÷           - n divided by           [10,5,3.33..,2.5,2,1.66..,1.42..,1.25,1.11..,1 ]
  %1        - modulo by 1            [ 0,0,0.33..,0.5,0,0.66..,0.42..,0.25,0.11..,0 ]
    <.      - less than 0.5?         [ 1,1,1     ,0  ,1,0     ,1     ,1   ,1     ,1 ]
      ¬     - not                    [ 0,0,0     ,1  ,0,1     ,0     ,0   ,0     ,0 ]
       ;1   - concatenate a 1        [ 0,0,0     ,1  ,0,1     ,0     ,0   ,0     ,0 , 1]
         T  - truthy indices         [            4    ,6                           ,11]
          Ṃ - minimum                4

注:の連結は1、単に例扱うためにあるnの一つである12または4結果がする必要があるときn+1‘R÷@%1<.¬TṂうも仕事を)。




2

Pyth、5バイト

fgy%Q

テストスイート

丸めビルトインはありません。最初の正の整数Tをチェックします。入力mod Tの2倍はT以上です。

説明:

fgy%Q
fgy%QTT    Implicit variable introduction.
f          Find the first positive integer T such that the following is truthy:
   %QT     Input % T
  y        Doubled
 g    T    Is greater than or equal to T

2

x86マシンコード、17バイト

このコードは、再利用可能な関数の形式で基本的な反復ソリューションを実装します。

31 F6                   xor    esi, esi
46                      inc    esi         ; set ESI (our temp register) to 1

                     Loop:
89 C8                   mov    eax, ecx    ; copy 'n' to EAX for division
46                      inc    esi         ; eagerly increment temp
99                      cdq                ; extend EAX into EDX:EAX
F7 F6                   div    esi         ; divide EDX:EAX by ESI
01 D2                   add    edx, edx    ; multiply remainder by 2
39 F2                   cmp    edx, esi    ; compare remainder*2 to temp
7C F4                   jb     Loop        ; keep looping if remainder*2 < temp

96                      xchg   eax, esi    ; put result into EAX (1 byte shorter than MOV)
C3                      ret

この関数はfastcall呼び出し規約に従い、単一のパラメーター(n)がECXレジスターに渡されます。戻り値(k)は、通常、EAXレジスタに返されます。

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


2

Java 8、42バイト

ラムダIntegerInteger

n->{for(int f=1;;)if(n%++f*2>=f)return f;}

オンラインで試す

謝辞

  • -1バイト、Kevin Cruijssenに感謝

4
次のように、最初のf=1を開始して使用することにより、バイトを保存できます。++ffn->{for(int f=1;;)if(n%++f*2>=f)return f;}
Kevin Cruijssen


1

Forth(gforth)、45バイト

: f 1 begin 1+ 2dup mod over 1+ 2/ >= until ;

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

コードの説明

: f             \ start a new word definition
  1             \ start a counter at 1
  begin         \ start an indefinite loop
    1+          \ add 1 to counter
    2dup mod    \ duplicate input value and counter, get remainder of input/counter
    over 1+ 2/  \ get counter/2 (add 1 to force rounding up)
    >=          \ check if remainder is greater than counter/2
  until         \ end loop if true, otherwise go back to beginning
;               \ end word definition

1

05AB1E、9バイト

∞.ΔIs/Dò‹

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

説明

∞.ΔIs/Dò‹ Full code
∞.Δ       Returns the first number for which the following code returns true
             -> stack is [n]
   Is     Push the input and swap the stack -> stack is [input, n]
     /    Divide both of them -> stack is [input/n]
      Dò  Duplicate and round the second -> stack is [input/n, rounded(input/n)]
        ‹ Check if input/n got larger by rounding -> stack is [bool]
             -> if bool is true, abort and return the current number

1

ロックスター、681バイト

Thought takes Patience and Control
While Patience is as high as Control
Let Patience be without Control

Give back Patience

Rock takes Art
Love is neverending
Sex is bottomless
Put Thought taking Art & Love into your head
If your head is Sex
Give back Art
Else
Limits are inspiration
Put Art with Limits without your head into the rubbish
Give back the rubbish


Listen to Chance
Questions are unstoppable
Until Questions is Chance
Build Questions up
Put Thought taking Chance, Questions into your mind
Answers are independence (but)
Put Questions over Answers into the world
Put Rock taking the world into the world
If your mind is as big as the world
Say Questions
Break it down

あなたはできる rockstar online試すますが、コードをコピーして貼り付ける必要があります。入力番号の入力を求められます。

ロックスターは明らかにゴルフ用に作られていないため、最低バイト数には行きませんでした。その代わりに、ロックンロールの歌詞を探しました。

説明:

これは、他のソリューション(python、java)と同じソリューションに基づいています。

Iterate up from 2:
if n % iterator >= ceil(n/2)
    return iterator

ただし、最初にモジュラスとシーリング関数を定義する必要があります。これは詩のために、Thought and Rockと呼ばれます。

以下は、異なる変数名を使用した詩的ではないバージョンと、構文が明確でない説明です。括弧はコメントを示します。

Modulus takes Number and Divisor
While Number is as high as Divisor
Put Number minus Divisor into Number
    (blank line ending While block)
Give back Number (return Number)
    (blank line ending function declaration)
Ceil takes Decimal
Put Modulus taking Decimal, 1 into Remainder
If Remainder is 0
Give back Decimal (return Decimal)
Else
Put Decimal with 1 minus Remainder into Result
Give back Result (return Result)
    (blank line ending if block)
    (blank line ending function declaration)
Listen to Input (Read from STDIN to Input)
Index is 1
Until Index is Input
Build Index up (Increment by 1)
Put Modulus taking Input, Index into LHS
Put Index over 2 into RHS
Put Ceil taking RHS into RHS
If LHS is as big as RHS
Say Index
Break it down (Break from loop)


0

Swift 3、51バイト

{n in(2..<n+2).filter{Float(n%$0)>=Float($0)/2}[0]}

いくつかの非常に奇妙な理由により、[0]オンラインでは機能しません。オンラインコンパイラー互換バージョンは.first!次のとおりです(代わりに使用します)。

{n in(2..<n+2).filter{Float(n%$0)>=Float($0)/2}.first!}

テストスイート(オンライン互換)。



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