g | x | w | all
Bytes Lang Time Link
055APLNARS250730T092550ZRosario
054Desmos250730T014057ZErikDaPa
042AWK250729T135937Zxrs
020Dyalog APL250728T234107ZAaron
020K ngn/k201222T193953Zcoltim
032Arturo230815T054256Zchunes
3375Vyxal s230811T140211Zlyxal
005Thunno 2 S230811T134415ZThe Thon
012MATL170624T121713ZSuever
042Perl 5 MListUtil=max p201223T025744ZXcali
025APL Dyalog Unicode201222T191142ZKamila S
072C# .NET Core170624T135327ZCharlie
072Java 8170626T085050ZKevin Cr
053R180109T230326ZGiuseppe
006Japt170624T122103ZShaggy
022Charcoal171202T113141ZNeil
005NewStack170626T081543ZGraviton
027Cubix170626T021910ZMickyT
043Haskell170624T120031Znimi
007Neim170625T135344ZOkx
050Python 2170624T144542ZDennis
145Python 2 PyPy170625T013916ZAnders K
030Pari/GP170624T175728Zalephalp
017Alice170624T231233ZNitrodon
037Charcoal170624T224201ZCharlie
007Husk170624T211515ZZgarb
nan170624T205321ZBrad Gil
053C gcc170624T184930ZConor O&
070Python 2 PyPy170624T160631ZDennis
008Pyth170624T113737ZLeaky Nu
031Stacked170624T180214ZConor O&
018J170624T173113ZLeaky Nu
030Mathematica170624T171813ZZaMoC
004Oasis170624T114745ZAdnan
059Python 3170624T113004ZLeaky Nu
024Retina170624T122151ZLeaky Nu
071Python 3170624T125650Z0xffcour
00805AB1E170624T123700ZDatboi
072Prolog SWI170624T124736ZLeaky Nu
074Lua170624T123955ZLeaky Nu
056PHP170624T113555ZJör
040JavaScript ES6170624T120237ZNeil
012Actually170624T114001ZLeaky Nu
010Brachylog170624T113504ZLeaky Nu
006Jelly170624T112517ZLeaky Nu

APL(NARS), 55 chars

{+/{⌈/⍵∼⍨∪×/¨{{⍵/z}¨{⍵{(⍺⍴2)⊤⍵}¨¯1+⍳2*⍵}↑⍴z←⍵}π⍵}¨2..⍵}

It would factor each element of the range 2..⍵, build from each array of factors the array of all combination, multiply each combination, make the set result ∪ (without a repeated element) for find all divisors, eliminate from array of divisors ⍵, find the max, sum all.

test:

  f←{+/{⌈/⍵∼⍨∪×/¨{{⍵/z}¨{⍵{(⍺⍴2)⊤⍵}¨¯1+⍳2*⍵}↑⍴z←⍵}π⍵}¨2..⍵}
  f 2
1
  f 4
4
  f 6
8
  f 15
41
  f 100
1690
  f 1000
165279

Desmos, 54 bytes

a=[1...i-1]
f(x)=[a[mod(i,a)=0].maxfori=[2...x]].total

Try this online!

AWK, 42 bytes

{for(i=1;i++<$1;y+=j)for(j=i;i%--j;);}$0=y

Attempt This Online!

{for(i=1;        # prevent div by zero
i++<$1;          # until input
y+=j)            # add highest
for(j=i;i%--j;); # eval zero at first hit
}$0=y            # set output

Dyalog APL, 20

{+/1↓{⊃¯2⌽∪⍵∨⍳⍵}¨⍳⍵}

Explanation

{+/1↓{⊃¯2⌽∪⍵∨⍳⍵}¨⍳⍵}
                 ⍳⍵   Indices from 1 to N
     {         }¨     For each of them
           ⍵∨⍳⍵           Find the greatest common divisor between the indices of that and the original argument
          ∪               Uniquify
       ¯2⌽                Rotate backwards twice; this puts the number we're after at the front, with the original number (the improper part) right after
      ⊃                   Grab that first one
   1↓                Drop the first element which is always 1
 +/                  And sum

K (ngn/k), 21 20 bytes

+/{|/&~1!x%!x}'2_!1+

Try it online!

Arturo, 32 bytes

$=>[∑map..2&=>[x:do factors&]]

Try it!

Vyxal s, 27 bitsv2, 3.375 bytes

ƛKṪt

Try it Online!

Explained

ƛKṪt­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌­
ƛ     # ‎⁡To each number 1..n
 KṪ   # ‎⁢Push the divisors of each number with that number removed
   t  # ‎⁣And get the last divisor remaining. Returns 0 on an empty list
💎

Created with the help of Luminespire.

Thunno 2 -S, 5 bytes

ı⁺FṫG

Try it online!

Explanation

ı⁺FṫG  # Implicit input
       # Decrement the input
ı      # Map over [1..input-1]:
 ⁺     #  Increment the number
  Fṫ   #  Push the proper divisors
    G  #  Maximum of this list
       # Sum the list
       # Implicit output

MATL, 12 bytes

q:Q"@Z\l_)vs

Try it at MATL Online

Explanation

        % Implicitly grab input (N)
q       % Subtract one
:       % Create an array [1...(N-1)]
Q       % Add one to create [2...N]
"       % For each element
  @Z\   % Compute the divisors of this element (including itself)
  l_)   % Grab the next to last element (the largest that isn't itself)
  v     % Vertically concatenate the entire stack so far
  s     % Sum the result

Perl 5 -MList::Util=max -p, 42 bytes

//,$\+=max grep$'%$_==0,1..$_-1for 2..$_}{

Try it online!

APL (Dyalog Unicode), 25 bytes

Lack of builtins is sad.

+/((⌈/∘⍸0=g|⊢)¨1+g←⍳1-⍨⊢)

Alternative dfn version: {+/{⌈/⍸0=⍵|⍨⍳1-⍨⍵}¨1+⍳1-⍨⍵}

Explanation:

+/((⌈/∘⍸0=g|⊢)¨1+g←⍳1-⍨⊢)
                 g←⍳1-⍨⊢   ⍝ numbers from 1 to ⍵-1 - define as `g`
               1+          ⍝ increment: numbers from 2 to ⍵.
              ¨            ⍝ for each of these...
        0=g|⊢              ⍝ mark evenly divisible indexes with 1
    ⌈/∘⍸                   ⍝ pick largest of proper divisors
+/                         ⍝ sum all

Try it online!

C# (.NET Core), 74 72 bytes

n=>{int r=0,j;for(;n>1;n--)for(j=n;--j>0;)if(n%j<1){r+=j;j=0;}return r;}

Try it online!

Java 8, 78 74 72 bytes

n->{int r=0,j;for(;n>1;n--)for(j=n;j-->1;)if(n%j<1){r+=j;j=0;}return r;}

Port of @CarlosAlejo's C# answer.

Try it here.

Old answer (78 bytes):

n->{int r=0,i=1,j,k;for(;++i<=n;r+=k)for(j=1,k=1;++j<i;k=i%j<1?j:k);return r;}

Try it here.

Explanation (of old answer):

n->{                    // Method with integer parameter and integer return-type
  int r=0,              //  Result-integers
      i=1,j,k;          //  Some temp integers
  for(;++i<=n;          //  Loop (1) from 2 to `n` (inclusive)
      r+=k)             //    And add `k` to the result after every iteration
    for(j=1,k=1;++j<i;  //   Inner loop (2) from `2` to `i` (exclusive)
      k=i%j<1?j:k       //    If `i` is dividable by `j`, replace `k` with `j`
    );                  //   End of inner loop (2)
                        //  End of loop (2) (implicit / single-line body)
  return r;             //  Return result-integer
}                       // End of method

R, 53 bytes

function(n){for(i in 2:n)F=F+(y=(i-1):1)[!i%%y][1]
F}

Try it online!

For each integer i in 2...n, computes the divisors of i in decreasing order with (y=(i-1):1)[!i%%y] and then selects the first, as that will be the largest, adding it to F, which is by default initialized to FALSE or 0.

Japt, 8+2=10 8 6 bytes

òâ1 xo

Test it


Explanation

    :Implicit input of integer U.
ò   :Generate an array of integers from 1 to U, inclusive
â   :Get the divisors of each number,
1   :  excluding itself.
x   :Sum the main array
o   :by popping the last element from each sub-array.
    :Implicit output of result

Charcoal, 22 bytes

IL⪫E…·²N×⌈E…¹ι⎇﹪ιλ⁰λ1ω

Try it online! Link is to verbose version of code. Explanation:

      ²                 Literal 2
       N                Input number
    …·                  Inclusive range
   E                    Map with variable `i`
            ¹           Literal 1
             ι          Variable `i`
           …            Exclusive range
          E             Map with variable `l`
                ι       Variable `i`
                 λ      Variable `l`
               ﹪        Modulo
              ⎇         Ternary
                  ⁰     If nonzero then zero
                   λ    If zero then variable `l`
         ⌈              Take the maximum
                    1   Arbitrary character
        ×               Repeat it
  ⪫                  ω  Join the strings together
 L                      Take the length
I                       Cast to string
                        Implicitly print

The Sum function has been added since the question was asked, reducing the code to 18 bytes:

IΣE…·²N⌈E…¹ι⎇﹪ιλ⁰λ

NewStack, 5 bytes

Luckily, there's actually a built in.

Nᵢ;qΣ

The breakdown:

Nᵢ       Add the first (user's input) natural numbers to the stack.
  ;      Perform the highest factor operator on whole stack.
   q     Pop bottom of stack.
    Σ    Sum stack.

In actual English:

Let's run an example for an input of 8.

Nᵢ: Make list of natural numbers from 1 though 8: 1, 2, 3, 4, 5, 6, 7, 8

;: Compute the greatest factors: 1, 1, 1, 2, 1, 3, 1, 4

q. Remove the first element: 1, 1, 2, 1, 3, 1, 4

Σ And take the sum: 1+1+2+1+3+1+4 = 13

Cubix, 27 39 bytes

?%\(W!:.U0IU(;u;p+qu.@Op\;;

Try it online!

Cubified

      ? % \
      ( W !
      : . U
0 I U ( ; u ; p + q u .
@ O p \ ; ; . . . . . .
. . . . . . . . . . . .
      . . .
      . . .
      . . .

Watch It Run

Haskell, 48 46 43 bytes

f 2=1
f n=until((<1).mod n)pred(n-1)+f(n-1)

Try it online!

Edit: @rogaos saved two bytes. Thanks!

Edit II: ... and @xnor another 3 bytes.

Neim, 7 bytes

𝐈Γ𝐅𝕙𝐠)𝐬

Explanation:

Example input: 6
𝐈        Inclusive range [1 .. input]
         stack: [1 2 3 4 5 6]
 Γ       For each...
  𝐅        Get factors
           stack: [[1] [1 2] [1 3] [1 2 4] [1 5] [1 2 3 6]]
   𝕙       Remove last element
           stack: [[] [1] [1] [1 2] [1] [1 2 3]]
    𝐠      Get greatest element
           stack: [0 1 1 2 1 3]
      )  End for each
       𝐬 Sum.
         stack: [8]
Implicit output: 8

Try it!

Python 2, 50 bytes

f=lambda n,k=2:n/k and(f(n,k+1),n/k+f(n-1))[n%k<1]

This is slow and can't even cope with input 15 on TIO.

Try it online!

However, memoization (thanks @musicman523) can be used to verify all test cases.

Try it online!

Alternate version, 52 bytes

At the cost of 2 bytes, we can choose whether to compute f(n,k+1) or n/k+f(n-1).

f=lambda n,k=2:n>1and(n%k and f(n,k+1)or n/k+f(n-1))

With some trickery, this works for all test cases, even on TIO.

Try it online!

Python 2 (PyPy), 145 bytes

Because turning code-golf competitions into fastest-code competitions is fun, here is an O(n) algorithm that, on TIO, solves n = 5,000,000,000 in 30 seconds. (Dennis’s sieve is O(n log n).)

import sympy
n=input()
def g(i,p,k,s):
 while p*max(p,k)<=n:l=k*p;i+=1;p=sympy.sieve[i];s-=g(i,p,l,n/l*(n/l*k+k-2)/2)
 return s
print~g(1,2,1,-n)

Try it online!

How it works

We count the size of the set

S = {(a, b) | 2 ≤ an, 2 ≤ b ≤ largest-proper-divisor(a)},

by rewriting it as the union, over all primes p ≤ √n, of

Sp = {(pd, b) | 2 ≤ dn/p, 2 ≤ bd},

and using the inclusion–exclusion principle:

|S| = ∑ (−1)m − 1 |Sp1 ∩ ⋯ ∩ Spm| over m ≥ 1 and primes p1 < ⋯ < pm ≤ √n,

where

Sp1 ∩ ⋯ ∩ Spm = {(p1pme, b) | 1 ≤ en/(p1pm), 2 ≤ bp1pm − 1e},
|Sp1 ∩ ⋯ ∩ Spm| = ⌊n/(p1pm)⌋⋅(p1pm − 1⋅(⌊n/(p1pm)⌋ + 1) − 2)/2.

The sum has Cn nonzero terms, where C converges to some constant that’s probably 6⋅(1 − ln 2)/π2 ≈ 0.186544. The final result is then |S| + n − 1.

Pari/GP, 36 30 bytes

n->sum(i=2,n,i/divisors(i)[2])

Try it online!

Alice, 17 bytes

/o
\i@/&w!qB;?+]k

Try it online!

Explanation

This is a standard format for a program which takes input as an integer, outputs an integer, and does everything else in cardinal mode.

The program tries to add up the highest proper divisor of every number from 0 to n. In the case of 0 and 1, the number added comes from the implicit zeros on the stack, so we don't have to bother skipping these cases.

i    Take input string (in ordinal mode)
&w   Implicitly convert into an integer, and push a return address n times.
     This starts the main loop, which will run a total of n+1 times.
!    Store the accumulator on the current tape cell.
q    Get the tape position. (initially zero)
B    Compute all divisors.
;    Remove the top of the stack (the number itself).
?    Copy accumulator back from tape.
+    Add to greatest proper divisor.
]    Move tape right.
k    Return to pushed return address.  The (n+1)st time through this loop, 
     there is nowhere to return to, and the program continues.
o    Output the integer (as a string in ordinal mode).
@    Terminate.

Charcoal, 37 bytes

A⁰βF…·²N«A⟦⟧δF⮌…¹ι«¿¬﹪ικ⊞δκ»A⁺β⌈δβ»Iβ

Try it online!

Link is to the verbose version. It took me almost all day to figure out how could I solve a non-ASCII-art-related question in Charcoal, but finally I got it and I am very proud of me. :-D

Yes, I am sure this can be golfed a lot. I just translated my C# answer and I am sure things can be done differently in Charcoal. At least it solves the 1000 case in a couple of seconds...

Husk, 7 bytes

ṁȯΠtptḣ

Try it online!

Explanation

Husk has no built-in for computing the divisors directly (yet), so I'm using prime factorization instead. The largest proper divisor of a number is the product of its prime factors except the smallest one. I map this function over the range from 2 to the input, and sum the results.

ṁȯΠtptḣ  Define a function:
      ḣ  Range from 1 to input.
     t   Remove the first element (range from 2).
ṁ        Map over the list and take sum:
 ȯ        The composition of
    p     prime factorization,
   t      tail (remove smallest prime) and
  Π       product.

Perl 6, 36 bytes

{sum map {max grep $_%%*,^$_},2..$_}

Test it

Expanded:

{
  sum
    map
    {
      max
        grep
        $_ %% *, # is the input to this block divisible by
        ^$_      # Range of possible divisors
    },
    2 .. $_
}

C (gcc), 53 bytes

x;s;f(n){for(s=0;n>1;--n){for(x=n;n%--x;);s+=x;}n=s;}

Try it online!

Comfortably an quickly passes all test cases.

Python 2 (PyPy), 73 71 70 bytes

n=input();r=[0]*n;d=1
while n:n-=1;r[d+d::d]=n/d*[d];d+=1
print sum(r)

Not the shortest Python answer, but this just breezes through the test cases. TIO handles inputs up to 30,000,000 without breaking a sweat; my desktop computer handles 300,000,000 in a minute.

At the cost of 2 bytes, the condition n>d could be used for a ~10% speed-up.

Thanks to @xnor for the r=[0]*n idea, which saved 3 bytes!

Try it online!

Pyth, 13 9 8 bytes

1 byte thanks to jacoblaw.

tsm*FtPh

Test suite.

How it works

The largest proper divisor is the product of the prime factors except the smallest one.

Stacked, 31 bytes

[2\|>[divisors:pop\MAX]map sum]

Try it online! (All testcases except for 1000, which exceeds the 60 second online time limit.)

Explanation

[2\|>[divisors:pop\MAX]map sum]
 2\|>                               range from 2 to the input inclusive
     [                ]map          map this function over the range
      divisors                      get the divisors of the number (including the number)
              :pop\                 pop a number off the array and swap it with the array
                   MAX              gets the maximum value from the array
                           sum      sum's all the max's

J, 18 bytes

[:+/1}.&.q:@+}.@i.

Try it online!

Mathematica, 30 bytes

Divisors[i][[-2]]~Sum~{i,2,#}&

Oasis, 4 bytes

Code:

nj+U

Try it online!

Explanation:

Extended version:

nj+00

    0   = a(0)
   0    = a(1)

a(n) =

n       # Push n
 j      # Get the largest divisor under n
  +     # Add to a(n - 1)

Python 3, 69 63 59 bytes

4 bytes thanks to Dennis.

f=lambda n:n-1and max(j for j in range(1,n)if n%j<1)+f(n-1)

Try it online!

I set the recursion limit to 2000 for this to work for 1000.

Retina, 31 24 bytes

7 bytes thanks to Martin Ender.

.+
$*
M!&`(1+)(?=\1+$)
1

Try it online!

How it works

The regex /^(1+)\1+$/ captures the largest proper divisor of a certain number represented in unary. In the code, the \1+ is turned to a lookahead syntax.

Python 3, 78 75 73 71 bytes

Not even close to Leaky nun's python answer in byte count.

f=lambda z:sum(max(i for i in range(1,y)if 1>y%i)for y in range(2,z+1))

Try it online!

05AB1E, 9 8 bytes

-1 Byte thanks to Leaky Nun's prime factor trick in his Pyth answer

L¦vyÒ¦PO

Try it online!

Explanation

L¦vyÒ¦PO
L¦       # Range [2 .. input]
  vy     # For each...
    Ò¦    # All prime factors except the first one
      P   # Product
       O  # Sum with previous results
         # Implicit print

Alternative 8 Byte solution (That doesnt work on TIO)

L¦vyѨθO    

and ofc alternative 9 Byte solution (That works on TIO)

L¦vyѨ®èO    

Prolog (SWI), 72 bytes

f(A,B):-A=2,B=1;C is A-1,f(C,D),between(2,A,E),divmod(A,E,S,0),B is D+S.

Try it online!

Lua, 74 bytes

c=0 for i=2,...do for j=1,i-1 do t=i%j<1 and j or t end c=c+t end print(c)

Try it online!

PHP, 56 bytes

for($i=1;$v||$argn>=$v=++$i;)$i%--$v?:$v=!$s+=$v;echo$s;

Try it online!

JavaScript (ES6), 40 bytes

f=(n,i=2)=>n<2?0:n%i?f(n,i+1):n/i+f(n-1)
<input type=number oninput=o.textContent=f(this.value)><pre id=o>

A number equals the product of its highest proper divisor and its smallest prime factor.

Actually, 12 bytes

u2x⌠÷R1@E⌡MΣ

Try it online!

Brachylog, 10 bytes

⟦bb{fkt}ᵐ+

Try it online!

Jelly, 6 bytes

ÆḌ€Ṫ€S

Try it online!

How it works

ÆḌ€Ṫ€S
ÆḌ€    map proper divisor (1 would become empty array)
           implicitly turns argument into 1-indexed range
   Ṫ€  map last element
     S sum