คำนวณจำนวนต่ำสุดที่ผลรวมของลำดับตัวเลขเกินค่าที่กำหนด


14

ให้คุณมีลำดับหมายเลขที่ไม่สิ้นสุดกำหนดดังนี้:

1: 1 = 1
2: 1 + 2 = 3
3: 1 + 3 = 4
4: 1 + 2 + 4 = 7
5: 1 + 5 = 6
6: 1 + 2 + 3 + 6 = 12
7: 1 + 7 = 8
...

ลำดับคือผลรวมของตัวหารของnรวมทั้งที่ 1 nและ

ป.ร. ให้ไว้เป็นจำนวนเต็มบวกxเป็น input คำนวณจำนวนต่ำสุดซึ่งจะผลิตมากขึ้นกว่าผลnx

กรณีทดสอบ

f(100) = 48, ∑ = 124
f(25000) = 7200, ∑ = 25389
f(5000000) = 1164240, ∑ = 5088960

ผลผลิตที่คาดหวัง

โปรแกรมของคุณควรส่งคืนทั้งสอง nและผลรวมของตัวหารเช่น:

$ ./challenge 100
48,124

กฎระเบียบ

นี่คือ code-golf ดังนั้นโค้ดที่สั้นที่สุดเป็นไบต์ในแต่ละภาษาจะชนะ


4
ลำดับนั้นเป็นเพียงผลรวมของnตัวหาร? คุณอาจต้องการระบุอย่างชัดเจน
Martin Ender

3
นอกจากนี้การตัดสินโดย "เอาท์พุทที่คาดหวัง" ของคุณคุณต้องการทั้งคู่n และ f(n)แต่คุณไม่ได้พูดอะไรในสเปค
Martin Ender

2
โบนัสไม่ดีโดยเฉพาะอย่างยิ่งเมื่อมันคลุมเครือ ฉันตัดสินใจที่จะลบมันเพื่อปกป้องสิ่งนี้จากการถูกลดระดับลง
Mr. Xcoder

2
คุณตรวจสอบอีกครั้งได้f(1000) = 48ไหม ผลรวมของตัวหาร48คือ124
caird coinheringaahing

3
เป็นการดีที่จะรออย่างน้อยหนึ่งสัปดาห์ก่อนที่จะรับคำตอบไม่เช่นนั้นคุณอาจไม่สนับสนุนวิธีแก้ไขปัญหาใหม่
Zgarb

คำตอบ:


8

Brachylogขนาด 9 ไบต์

∧;S?hf+S>

โปรแกรมนี้จะใช้เวลาการป้อนข้อมูลจาก "เอาท์พุทตัวแปร" .และเอาท์พุทที่ ?"ตัวแปร" ลองออนไลน์!

คำอธิบาย

∧;S?hf+S>
∧;S        There is a pair [N,S]
   ?       which equals the output
    h      such that its first element's
     f     factors'
      +    sum
       S   equals S,
        >  and is greater than the input.

ตัวแปรโดยนัยNจะถูกระบุในลำดับที่เพิ่มขึ้นดังนั้นค่าทางกฎหมายที่ต่ำที่สุดจึงถูกใช้สำหรับเอาท์พุท


10

เยลลี่ , 18 12 11 10 ไบต์

1Æs>¥#ḢṄÆs

ลองออนไลน์!

-1 ไบต์ขอบคุณMr. Xcoder !

มันทำงานอย่างไร

1Æs>¥#ḢṄÆs - Main link. Argument: n (integer)
1   ¥#     - Find the first n integers where...
 Æs        -   the divisor sum
   >       -   is greater than the input
       Ṅ   - Print...
      Ḣ    -   the first element
        Æs - then print the divisor sum

คุณช่วยอธิบายได้ไหมว่าเหตุใดจึง1จำเป็นและวิธีการ¥กระทำ
dylnan

1
@dylnan The 1บอก#ให้เริ่มนับจาก 1 และ¥ใช้การเชื่อมโยงสองอันก่อนหน้า ( Æsและ>) และใช้มันเป็น dyad (เช่นมีสองอาร์กิวเมนต์) โดยอาร์กิวเมนต์ที่เหลือคือการวนซ้ำและอาร์กิวเมนต์ที่ถูกต้องเป็นอินพุต
caird coinheringaahing

โอ้นั่นสมเหตุสมผลแล้ว #เคยสับสนกับฉันมาก่อนในบางกรณี
dylnan

4

ภาษา Wolfram (Mathematica) , 53 ไบต์

{#,f@#}&@@Select[Range[x=#]+1,(f=Tr@*Divisors)@#>x&]&

ลองออนไลน์!

พยายามค่าทั้งหมดระหว่าง 2 ถึง x + 1 โดยที่ x คืออินพุต

(การSelectส่งคืนรายการของค่าทั้งหมดที่ใช้งานได้ แต่ฟังก์ชั่น{#,f@#}&รับสิ่งเหล่านี้ทั้งหมดเป็นอินพุตจากนั้นจะละเว้นอินพุตทั้งหมดยกเว้นค่าแรก)



4

Husk , 12 11 ไบต์

§eVḟ>⁰moΣḊN

-1 ไบต์ขอบคุณ @Zgarb!

ลองออนไลน์!


ฉลาด! แปลก แต่วิธีการ,ทำงาน (หรือการอนุมานใช้เวลานานเกินไป)
იოო

มันอนุมานชนิด แต่ผลลัพธ์รายการไม่มีที่สิ้นสุด มันอาจเกิดจากการโอเวอร์โหลดของḟที่ใช้จำนวนเต็มเป็นอาร์กิวเมนต์ที่สอง แต่นั่นเป็นเพียงการคาดเดา
Zgarb


4

Japt 15 ไบต์

[@<(V=Xâ x}a V]

ลองมัน


คำอธิบาย

Uการป้อนข้อมูลโดยนัยของจำนวนเต็ม []เป็น wrapper อาร์เรย์ของเรา สำหรับองค์ประกอบแรก@ }aคือฟังก์ชั่นที่ทำงานอย่างต่อเนื่องจนกว่ามันจะส่งกลับค่าความจริงผ่านตัวเองจำนวนเต็มที่เพิ่มขึ้น (เริ่มต้นที่ 0) ในแต่ละครั้งและส่งออกมูลค่าสุดท้ายของจำนวนเต็มนั้น âได้รับหารของจำนวนเต็มปัจจุบัน ( X) สรุปพวกเขาและผลที่ได้รับมอบหมายให้ตัวแปรx ตรวจสอบว่ามีค่าน้อยกว่า องค์ประกอบที่สองในอาร์เรย์นั้นก็เป็นเพียงแค่V<UVV


4

Clojure , 127 ไบต์

(defn f[n](reduce +(filter #(zero?(rem n %))(range 1(inc n)))))
(defn e[n](loop[i 1 n n](if(>(f i)n){i,(f i)}(recur(inc i)n))))

ลองออนไลน์!

ขอบคุณ @steadybox สำหรับ -4 ไบต์!


1
ยินดีต้อนรับสู่เว็บไซต์!
caird coinheringaahing

ช่องว่างบางช่องสามารถลบออกได้เพื่อบันทึกสองสามไบต์ ลองออนไลน์!
Steadybox

ในกรณีนี้reduceสามารถถูกแทนที่ด้วยapplyฟังก์ชั่นeอาจแสดงเป็นฟังก์ชั่นที่ไม่ระบุชื่อผ่านทาง#(...)ไวยากรณ์คุณไม่จำเป็นต้องตั้งชื่อที่ Code Golf จะสั้นกว่า#(=(rem n %)0) #(zero?(rem n %))และจำไว้ว่า,เป็นช่องว่างและสามารถลบออกได้ในกรณีนี้ตามด้วย(ดังนั้นมันจะถูกวิเคราะห์อย่างถูกต้อง
NikoNyrh

@NikoNyrh ดีใจที่ได้พบเพื่อนผู้ปิดบังฉันจะแก้ไขโพสต์นี้ในไม่ช้า
Alonoaky

3

ทับทิม 58 ไบต์

โปรแกรมเต็มเพราะฉันไม่แน่ใจว่าได้รับอนุญาต lambdas หรือไม่ /ยัก

gets
$.+=1until$_.to_i.<v=(1..$.).sum{|n|$.%n<1?n:0}
p$.,v

ลองออนไลน์!

คำอธิบาย

gets     # read line ($_ is used instead of v= because it cuts a space)
$.+=1    # $. is "lines read" variable which starts at 1 because we read 1 line
    until     # repeat as long as the next part is not true
$_.to_i  # input, as numeric
  .<v=   # is <, but invoked as function to lower operator prescedence
  (1..$.)        # Range of 1 to n
  .sum{|n|       # .sum maps values into new ones and adds them together
     $.%n<1?n:0  # Factor -> add to sum, non-factor -> 0
  }
p$.,v    # output n and sum

3
แลมบ์ดาได้รับอนุญาตอย่างแน่นอน
Giuseppe

3

JavaScript (ES6), 61 58 ไบต์

f=(n,i=1,s=j=0)=>j++<i?f(n,i,i%j?s:s+j):s>n?[i,s]:f(n,++i)
<input type=number min=0 oninput=o.textContent=f(this.value)><pre id=o>

แก้ไข: บันทึกแล้ว 3 ไบต์ขอบคุณ @Arnauld


ฉันได้รับ "ข้อผิดพลาดของสคริปต์" เมื่อป้อนค่ามากกว่า 545
StudleyJr

ลองใช้ Safari เห็นได้ชัดว่ามันรองรับ Tail Call Optimization (หรือหากคุณสามารถค้นหาได้ Chrome บางรุ่นจะเปิดใช้งานผ่าน "ฟีเจอร์การทดลอง JavaScript")
Neil



2

SOGL V0.12, 14 bytes

1[:Λ∑:A.>?ao←I

Try it Here!

Explanation:

1               push 1
 [              while ToS != 0
  :Λ              get the divisors
    ∑             sum
     :A           save on variable A without popping
       .>?  ←     if greater than the input
          ao        output the variable A
            ←       and stop the program, implicitly outputting ToS - the counter
             I    increment the counter


2

MATL, 12 bytes

`@Z\sG>~}@6M

Try it online!

Explanation

`      % Do...while
  @    %   Push iteration index (1-based)
  Z\   %   Array of divisors
  s    %   Sum of array
  G    %   Push input
  >~   %   Greater than; logical negate. This is the loop condition
}      % Finally (execute on loop exit)
  @    %   Push latest iteration index
  6M   %   Push latest sum of divisors again
       % End (implicit). Run new iteration if top of the stack is true
       % Display stack (implicit)




2

Factor, 88

USE: math.primes.factors [ 0 0 [ drop 1 + dup divisors sum pick over > ] loop rot drop ]

Brute-force search. It's a quotation (lambda), call it with x on the stack, leaves n and f(n) on the stack.

As a word:

: f(n)>x ( x -- n f(n) )
  0 0 [ drop 1 + dup divisors sum pick over > ] loop rot drop ;

2

Python 3, 163 bytes

def f(x):
    def d(x):return[i for i in range(1,x+1) if x%i==0]
    return min(i for i in range(x) if sum(d(i)) >x),sum(d(min(i for i in range(x) if sum(d(i)) >x)))

3
Hello and welcome to PPCG; nice first post! From a golfing aspect, you could save some bytes by removing whitespace, using lambda functions, collapsing everything onto one line and not repeating yourself. We also usually link to an online testing environment, like for example TIO (105 bytes, using the techniques described above.)
Jonathan Frech

@JonathanFrech: Excellent comment. Thanks for your patience with noobies in general and noob in particular ;)
Eric Duminil

2

Python 3, 100 bytes

d=lambda y:sum(i+1for i in range(y)if y%-~i<1)
f=lambda x:min((j,d(j))for j in range(x+1)if x<=d(j))

Try it online!

Thanks to Jonathan Frech's comment on the previous python 3 attempt, I have just greatly expanded my knowledge of python syntax. I'd never have thought of the -~i for i+1 trick, which saves two characters.

However, that answer is 1) not minimal and 2) doesn't work for x=1 (due to an off-by-one error which is easy to make while going for brevity; I suggest everyone else check their answers for this edge case!).

Quick explanation: sum(i+1for i in range(y)if y%-~i<1) is equivalent to sum(i for i in range(1,y+1)if y%i<1) but saves two characters. Thanks again to Mr. Frech.

d=lambda y:sum(i+1for i in range(y)if y%-~i<1) therefore returns the divisors of y.

f=lambda x:min((j,d(j))for j in range(x+1)if x<=d(j)) is where I really did work. Since comparing a tuple works in dictionary order, we can compare j,d(j) as easily as we can compare j, and this lets us not have to find the minimal j, store it in a variable, and /then/ compute the tuple in a separate operation. Also, we have to have the <=, not <, in x<=d(j), because d(1) is 1 so if x is 1 you get nothing. This is also why we need range(x+1) and not range(x).

I'd previously had d return the tuple, but then I have to subscript it in f, so that takes three more characters.


1
Welcome to the site and nice first post. You can get to 98 bytes by removing the f= as anonymous functions are perfectly acceptable here!
caird coinheringaahing

You can't call an anonymous function from another line of code, is the problem -- I have a separate print(f(100)) statement to test that the function works.
Michael Boger

That's not a problem here. It's perfectly acceptable and works to not include the f= in your byte count, and is a good way to golf in Python. Check this for more golfing tips in Python!
caird coinheringaahing

Hm. I can equal, but not better, my submission by appending q=range and replacing range with q in both existing instances. Sadly, this doesn't improve it and since lambda is a keyword I can't use it for that, I'd have to do exec() tricks wasting too many characters.
Michael Boger

@MichaelBoger Well, you can call an anonymous function in Python; lambda expressions do not have to be assigned to a variable.
Jonathan Frech






โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.