# Calculate the lowest number where the sum of the sequence of numbers exceeds a given value

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:

Try it online!

# PHP, 69 bytes

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

## 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)))))

# 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))))

Try it online!

thanks to @steadybox for -4 bytes!

• Welcome to the site! – caird coinheringaahing Dec 3 '17 at 15:31
• Some whitespace can be removed to save a few bytes. Try it online! – Steadybox Jan 14 '18 at 23:27
• In this case reduce can be replaced by apply, also the function e could 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. – NikoNyrh Jan 17 '18 at 9:34
• @NikoNyrh nice to meet a fellow clojurist, i'll edit this post soon – Alonoaky Jan 17 '18 at 11:30

# Japt, 15 bytes

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

Try it

## 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.

Try it online!

# 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
• Lambdas are certainly allowed. – Giuseppe Dec 3 '17 at 0:32

f x=[(i,s)|i<-[1..],s<-[sum[d|d<-[1..i],imodd<1]],s>x]!!0

-1 byte, thanks to @nimi!

Try it online!

# R, 71 bytes

function(x)for(n in 1:x){z=sum(which(n%%1:n==0));if(z>x)return(c(n,z))}

Try it online!

• 59 bytes but it will stack overflow for large x. – Giuseppe Dec 3 '17 at 0:17

# Wolfram Language (Mathematica), 53 bytes

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

Try it online!

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.)

# Gaia, 11 bytes

dΣ@>
↑#(:dΣ

Try it online!

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.

# 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)

# 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);}

Try it online!

# 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

# APL (Dyalog), 32 bytes

{⍺<o←+/o/⍨0=⍵|⍨o←⍳⍵:⍵,o⋄⍺∇⍵+1}∘0

Try it online!

⍵o⍺⍵.