Given you have an infinite sequence of numbers defined as follows:
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
...
The sequence is the sum of the divisors of n, including 1 and n.
Given a positive integer x as input, calculate the lowest number n which will produce a result greater than x.
Test cases
f(100) = 48, ∑ = 124
f(25000) = 7200, ∑ = 25389
f(5000000) = 1164240, ∑ = 5088960
Expected Output
Your program should return both n and the sum of its divisors, like so:
$ ./challenge 100
48,124
Rules
This is code-golf so the shortest code in bytes, in each language wins.
27 Answers 27
Jelly, (削除) 18 (削除ここまで) (削除) 12 (削除ここまで) (削除) 11 (削除ここまで) 10 bytes
1Æs>\#ḢṄÆs
-1 byte thanks to Mr. Xcoder!
How it works
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
-
\$\begingroup\$ Could you explain why the
1is necessary and how the¥acts? \$\endgroup\$dylnan– dylnan2017年12月03日 22:54:37 +00:00Commented Dec 3, 2017 at 22:54 -
1\$\begingroup\$ @dylnan The
1tells#to start counting from 1 and the¥takes the previous two links (Æsand>) and applies them as a dyad (i.e. with two arguments), with the left argument being the iteration, and the right argument being the input. \$\endgroup\$2017年12月03日 23:27:01 +00:00Commented Dec 3, 2017 at 23:27 -
\$\begingroup\$ Oh, that makes sense now.
#had been a bit confusing to me before in some cases. \$\endgroup\$dylnan– dylnan2017年12月04日 04:45:13 +00:00Commented Dec 4, 2017 at 4:45
Brachylog, 9 bytes
∧;S?hf+S>
This program takes input from the "output variable" ., and outputs to the "input variable" ?.
Try it online!
Explanation
∧;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.
The implicit variable N is enumerated in increasing order, so its lowest legal value is used for the output.
Wolfram Language (Mathematica), 53 bytes
{#,f@#}&@@Select[Range[x=#]+1,(f=Tr@*Divisors)@#>x&]&
Tries all values between 2 and x+1, where x is the input.
(The Select returns a list of all values that work, but the function {#,f@#}& takes all of these as inputs, and then ignores all its inputs but the first.)
-
\$\begingroup\$ Clever! Weird though how
,doesn't work (or inference takes too long?). \$\endgroup\$ბიმო– ბიმო2017年12月02日 21:28:16 +00:00Commented Dec 2, 2017 at 21:28 -
\$\begingroup\$ It does infer a type, but outputs an infinite list. It may be caused by the overloading of ḟ that takes an integer as the second argument, but that's just a guess. \$\endgroup\$Zgarb– Zgarb2017年12月02日 21:31:20 +00:00Commented Dec 2, 2017 at 21:31
R, 73 bytes
n=scan();while(1){d=(x=1:T)[!T%%x];if(sum(d)>n)break;T=T+1};cat(T,sum(d))
Outgolfed by duckmayr.
-
\$\begingroup\$ 71 bytes codegolf.stackexchange.com/a/149747/76266 \$\endgroup\$duckmayr– duckmayr2017年12月02日 21:07:17 +00:00Commented Dec 2, 2017 at 21:07
Japt, 15 bytes
[@<(V=Xâ x}a V]
Explanation
Implicit input of integer U. [] is our array wrapper. For the first element, @ }a is a function that run continuously until it returns a truthy value, passing itself an incrementing integer (starting at 0) each time, and outputting the final value of that integer. â gets the divisors of the current integer (X), x sums them and that result is assigned to variable V. < checks if U is less than V. The second element in the array is then just V.
Clojure, 127 bytes
(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))))
thanks to @steadybox for -4 bytes!
-
1\$\begingroup\$ Welcome to the site! \$\endgroup\$2017年12月03日 15:31:18 +00:00Commented Dec 3, 2017 at 15:31
-
\$\begingroup\$ Some whitespace can be removed to save a few bytes. Try it online! \$\endgroup\$Steadybox– Steadybox2018年01月14日 23:27:33 +00:00Commented Jan 14, 2018 at 23:27
-
\$\begingroup\$ In this case
reducecan be replaced byapply, also the functionecould be expressed as an anonymous function via the#(...)syntax, you don't need to name it at Code Golf.#(=(rem n %)0)is shorter than#(zero?(rem n %)). And remember that,is whitespace, and can be removed in this case as it is followed by(, so it will be parsed correctly. \$\endgroup\$NikoNyrh– NikoNyrh2018年01月17日 09:34:36 +00:00Commented Jan 17, 2018 at 9:34 -
\$\begingroup\$ @NikoNyrh nice to meet a fellow clojurist, i'll edit this post soon \$\endgroup\$Alonoaky– Alonoaky2018年01月17日 11:30:52 +00:00Commented Jan 17, 2018 at 11:30
Ruby, 58 bytes
Full program because I'm not sure if lambdas are allowed. /shrug
gets
$.+=1until$_.to_i.<v=(1..$.).sum{|n|$.%n<1?n:0}
p$.,v
Explanation
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\$\begingroup\$ Lambdas are certainly allowed. \$\endgroup\$Giuseppe– Giuseppe2017年12月03日 00:32:56 +00:00Commented Dec 3, 2017 at 0:32
JavaScript (ES6), (削除) 61 (削除ここまで) 58 bytes
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>
Edit: Saved 3 bytes thanks to @Arnauld.
-
\$\begingroup\$ I get "Script error." when inputting a value over 545 \$\endgroup\$StudleyJr– StudleyJr2017年12月04日 16:53:41 +00:00Commented Dec 4, 2017 at 16:53
-
\$\begingroup\$ Try using Safari; apparently it supports Tail Call Optimisation. (Or if you can find them, some versions of Chrome enable it via the "Experimental JavaScript features".) \$\endgroup\$Neil– Neil2017年12月04日 16:59:46 +00:00Commented Dec 4, 2017 at 16:59
05AB1E, 11 bytes
>LʒÑO‹}нDÑO
Leaves the output on the stack, as allowed per meta consensus. I added ) for the sake of visualization, but the program also implicitly prints the top of the stack.
SOGL V0.12, 14 bytes
1[:Λ∑:A.>?ao←I
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
C, (削除) 79 (削除ここまで) 78 bytes
i,n,s;f(x){for(i=n=s=0;x>s;s+=n%++i?0:i)i-n||(++n,i=s=0);printf("%d %d",n,s);}
-
\$\begingroup\$ Suggest
i=s=!++ninstead of++n,i=s=0\$\endgroup\$ceilingcat– ceilingcat2019年11月09日 02:11:26 +00:00Commented Nov 9, 2019 at 2:11
MATL, 12 bytes
`@Z\sG>~}@6M
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)
Gaia, 11 bytes
dΣ@>
↑#(:dΣ
Leaves the output on the stack, as allowed per meta consensus. I added €. for the sake of visualization, but the program also implicitly prints the top of the stack.
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 ;
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\$\begingroup\$ 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.) \$\endgroup\$Jonathan Frech– Jonathan Frech2017年12月02日 23:46:12 +00:00Commented Dec 2, 2017 at 23:46
-
\$\begingroup\$ @JonathanFrech: Excellent comment. Thanks for your patience with noobies in general and
noobin particular ;) \$\endgroup\$Eric Duminil– Eric Duminil2017年12月03日 14:04:08 +00:00Commented Dec 3, 2017 at 14:04
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))
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\$\begingroup\$ 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! \$\endgroup\$2017年12月03日 21:59:01 +00:00Commented Dec 3, 2017 at 21:59 -
\$\begingroup\$ 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. \$\endgroup\$Sam Howard– Sam Howard2017年12月03日 22:02:54 +00:00Commented Dec 3, 2017 at 22:02
-
\$\begingroup\$ 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! \$\endgroup\$2017年12月03日 22:05:22 +00:00Commented Dec 3, 2017 at 22:05 -
\$\begingroup\$ Hm. I can equal, but not better, my submission by appending
q=rangeand replacingrangewithqin 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. \$\endgroup\$Sam Howard– Sam Howard2017年12月04日 15:32:52 +00:00Commented Dec 4, 2017 at 15:32 -
\$\begingroup\$ @MichaelBoger Well, you can call an anonymous function in Python; lambda expressions do not have to be assigned to a variable. \$\endgroup\$Jonathan Frech– Jonathan Frech2017年12月04日 16:10:40 +00:00Commented Dec 4, 2017 at 16:10
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
-
\$\begingroup\$ 77 bytes. \$\endgroup\$Jonathan Frech– Jonathan Frech2017年12月04日 16:14:17 +00:00Commented Dec 4, 2017 at 16:14
-
\$\begingroup\$ 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. \$\endgroup\$Sam Howard– Sam Howard2018年07月15日 07:05:19 +00:00Commented Jul 15, 2018 at 7:05
Java (OpenJDK 8), 91 bytes
i->{for(int j=0;j++<i;)for(int k=0,l=0;k++<j;)if((l+=j%k<1?k:0)>i)return k+","+l;return"";}
Try it online! (timeout on third test case)
-
\$\begingroup\$ 89 bytes \$\endgroup\$ceilingcat– ceilingcat2019年11月09日 02:07:13 +00:00Commented Nov 9, 2019 at 2:07
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)))))
PHP, 69 bytes
for(;$argv[1]>=$t;)for($t=$j=++$i;--$j;)$t+=$i%$j?0:$j;echo$i,',',$t;
ns divisors? You'll probably want to state that explicitly. \$\endgroup\$nandf(n), but you don't say so anywhere in the specification. \$\endgroup\$f(1000) = 48? The divisor sum of48is124\$\endgroup\$