计算数字序列之和超过给定值的最低数字


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作为输入,请计算n将产生大于的结果的最小数字x

测试用例

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

预期产量

你的程序应该返回 n和其约数的总和,如下所示:

$ ./challenge 100
48,124

规则

这是代码高尔夫球,因此每种语言中以字节为单位的最短代码胜出。


4
该序列只是ns个除数的总和吗?您可能需要明确声明。
马丁·恩德

3
另外,根据您的“预期输出”来判断,您希望同时使用n f(n),但是在规范中的任何地方都没有这样说。
Martin Ender

2
奖金是不好的,尤其是在模糊的时候。我决定将其删除,以防止其被低估。
Xcoder先生17年

2
您可以重新检查f(1000) = 48吗?除数的总和48124
caird coinheringaahing

3
最好等待至少一周再接受答案,否则您可能不愿使用新的解决方案。
Zgarb

Answers:


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个字节感谢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 '17

1
@dylnan的1讲述#,从1开始计数,¥将前两个链接(Æs>),并将其应用于一个对子(即两个参数),用左手的说法是迭代,而右边的参数被输入。
caird coinheringaahing

哦,这很有意义。#在某些情况下,以前让我有些困惑。
dylnan '17



4

外壳12 11字节

§eVḟ>⁰moΣḊN

-1个字节,感谢@Zgarb!

在线尝试!


聪明!奇怪的是,,它不起作用(或者推理需要太长时间?)。
ბიმო

它确实推断出类型,但是输出无限列表。这可能是由于the的重载引起的,该重载以整数作为第二个参数,但这只是一个猜测。
Zgarb


4

Japt,15个字节

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

尝试一下


说明

整数的隐式输入U[]是我们的数组包装器。对于第一个元素,它@ }a是一个连续运行的函数,直到它返回真实值,每次都向自身传递一个递增的整数(从0开始),并输出该整数的最终值。â获取当前整数(X)的除数,对其x求和,然后将结果分配给variable V<检查是否U小于V。数组中的第二个元素就是just V


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字节

完整程序,因为我不确定是否允许使用lambda。/耸肩

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
当然可以使用Lambda。
朱塞佩

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>

编辑:由于@Arnauld,节省了3个字节。


我收到“脚本错误”。输入超过545的值时
StudleyJr

尝试使用Safari;显然,它支持尾部呼叫优化。(或者,如果可以找到它们,则某些版本的Chrome可以通过“实验性JavaScript功能”启用它。)
尼尔(Neil



2

SOGL V0.12,14字节

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

在这里尝试!

说明:

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字节

`@Z\sG>~}@6M

在线尝试!

说明

`      % 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

2

Python 2, 81 bytes

def f(n):
 a=b=0
 while b<n:
	a+=1;i=b=0
	while i<a:i+=1;b+=i*(a%i<1)
 return a,b

Try it online!



Replacing the tabs with two spaces makes this work in python 3 at 83 bytes, although to try it I had to put parentheses in the print statement. You can also replace the return statement with a print statement and not need an auxiliary function to print it; the bytes stay the same.
Michael Boger



0

Clojure, 102 bytes

#(loop[i 1](let[s(apply +(for[j(range 1(inc i)):when(=(mod i j)0)]j))](if(> s %)[i s](recur(inc i)))))

0

PHP, 69 bytes

for(;$argv[1]>=$t;)for($t=$j=++$i;--$j;)$t+=$i%$j?0:$j;echo$i,',',$t;

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.