राउंड मी, हेल्प मी


23

एक इनपुट को देखते हुए n, आपके प्रोग्राम या फ़ंक्शन को सबसे छोटे पॉजिटिव पूर्णांक का उत्पादन करना चाहिए, kजो कि nनिकटतम कई के लिए गोल से kअधिक है n

उदाहरण।

एक इनपुट को देखते हुए 20, आउटपुट मान होना चाहिए 3:

  • के सबसे पास 1है 20, जो से अधिक नहीं है 20

  • के सबसे पास 2है 20, जो से अधिक नहीं है 20

  • के निकटतम गुणज 3है 21, जो है की तुलना में अधिक 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
  • गोलाई को इस तरह से किया जाता है कि यदि दो गुणक kसमान रूप से दूर हैं n, तो बड़ा चुना जाता है ( "गोल आधा ऊपर" )। इस तरह, हर विषम nका उत्पादन होता है 2
  • यह , इसलिए प्रत्येक भाषा में सबसे कम कोड जीतता है।

मैंने आपके परीक्षण मामलों का प्रारूप संपादित किया है ताकि इसे पढ़ने में आसान हो, और अधिक संक्षिप्त हो। मुझे बताएं कि क्या आपको इससे कोई समस्या है, या यदि कोई नया उदाहरण बंद है। :)
DJMcMayhem

@ शगुन हो गया! मैंने सूची से 500 ऑड्स और 450 evens निकाले।
फायरफ्लेम 241

क्या इस अनुक्रम के लिए एक oeis लिंक है?
जेम्स के

@JamesK जब मैंने पहले खोजा तो मुझे एक नहीं मिला। संभवतः OEIS खाते वाला कोई व्यक्ति एक बना सकता है?
फायरफ्लेम २४१ १

जवाबों:



9

जाप , 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: जाप में ऊपर या नीचे चक्कर लगाने के लिए बिल्ट-इन भी है :)
Shaggy

5
मुझे पता था कि यह सुविधा किसी दिन काम आएगी: D
ETHproductions

@ नटखट है कि पागल है! o_o_o
आउटगॉल्फ

@ ओलिवर: यह मुझे फंक्शन के तरीकों के साथ पकड़ पाने के लिए अधिक आश्वस्त करता है, अब - मेरा खुद का संस्करण 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

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

n=>g=x=>n%x>=x/2?x:g(-~x)
  • 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 से सभी नंबरों का परीक्षण करें (इसे चलाने के लिए एक मिनट दें):


5

प्रोटॉन , 33 बाइट्स

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

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


मैं प्रोटॉन के बारे में कुछ नहीं जानता, लेकिन ऐसा लगता है कि आप 3 बाइट्स बचा सकते हैं: इसे ऑनलाइन आज़माएं!
जफर

शायद एक संयोग है, लेकिन यह पूरी तरह से अमानवीय समाधान के रूप में एक ही है ...: पी
एरिक आउटगोल्फर

@EriktheOutgolfer हमने इसे 37-बाइटर के साथ एक ही समय में (वास्तव में निंजा मैं उसे कुछ सेकंड के लिए) पोस्ट किया था, क्योंकि हाइपर ऑपरेटरों को परेशान करता था, और जब उन्होंने तय किया कि हम दोनों को अपडेट कर चुके हैं।
श्री एक्सकोडर

उह, मैं निंजा आपको IIRC चाहिए। : पी
बिलकुल अमानवीय

@totallyhuman तुम निंजा मुझे एक 41-बटर के साथ चाहते थे। मैंने पहले 37 बटर पोस्ट किया और निंजा आपको कुछ सेकंड के लिए एक साथ रखेगा।
श्री एक्सकोडर



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में से एक है 1, 2या 4परिणाम होने की जरूरत है जब n+1( ‘R÷@%1<.¬TṂभी काम होगा)।




2

अजगर, 5 बाइट्स

fgy%Q

परीक्षण सूट

कोई राउंडिंग बिलिन नहीं, बस पहले पॉजिटिव पूर्णांक T की जाँच करें, जहाँ डबल इनपुट मॉड T 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

फ़ंक्शन फास्टकॉल कॉलिंग कन्वेंशन का अनुसरण करता है , ताकि रजिस्टर nमें एकल पैरामीटर ( ) पास हो ECX। रिटर्न मान ( k), सामान्य रूप से, EAXरजिस्टर में लौटाया जाता है ।

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


2

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

से लैम्ब्डा Integerके लिए Integer

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

यह ऑनलाइन की कोशिश करो

स्वीकृतियाँ

  • केविन क्रूज़सेन के लिए धन्यवाद

4
आप शुरू करने से एक बाइट को बचा सकता है f=1और का उपयोग कर ++fपहली पर f: इस तरह,n->{for(int f=1;;)if(n%++f*2>=f)return f;}
केविन Cruijssen


1

फोर्थ (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

आप रॉकस्टार ऑनलाइन कोशिश कर सकते हैं , लेकिन आपको कोड को कॉपी और पेस्ट करना होगा। यह आपको एक इनपुट नंबर के लिए संकेत देगा।

मैं सबसे कम बाइट की गिनती के लिए नहीं गया, क्योंकि रॉकस्टार स्पष्ट रूप से गोल्फ के लिए नहीं बना है, इसलिए इसके बजाय मैंने रॉक 'एन' रोल गीतों के लिए जाने की कोशिश की।

स्पष्टीकरण:

यह अन्य (अजगर, जावा) के समान समाधान पर आधारित है:

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

पहले मुझे मापांक और छत के कार्यों को परिभाषित करने की आवश्यकता है, जो कि कविता के लिए थॉट और रॉक कहलाते हैं।

नीचे अलग-अलग चर नामों के साथ एक कम काव्य संस्करण है, और स्पष्टीकरण जहां वाक्यविन्यास अस्पष्ट है। कोष्ठक टिप्पणियों को निरूपित करता है।

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

जेली , 18 बाइट्स

ɓ÷Ḟ,¥Ċ$×ạÐṂ⁸Ṁ>⁸µ1#

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

पूरा कार्यक्रम।



@totallyhuman कभी-कभी यह एक फ़ंक्शन के रूप में भी काम करता है।
आउटगॉल्फ

0

स्विफ्ट 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!}

टेस्ट सूट (ऑनलाइन-संगत)।



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