# Logarithmic Incrementation

In this challenge you will write a function that takes a list (ordered set) containing real numbers (the empty list is an exception, as it has nothing) and calculates $$f(x)=\begin{cases}1 & \text{if } |x|=0 \\ x_1+1 & \text{if } |x|=1 \\ \log_{|x|}\sum_{n=1}^{|x|}{|x|}^{x_n} & \text{otherwise} \end{cases}$$ where $$\|x|\$$ is the length of list $$\x\$$ and $$\x_n\$$ is the $$\n^\text{th}\$$ item in the list $$\x\$$ using 1-based indexing. But why is this called “logarithmic incrementation”? Because there is an interesting property of the function: $$f(\{n,n\})=n+1$$ Amazing, right? Anyways, let’s not go off on a tangent. Just remember, this is , so the shortest answer wins! Oh, right, I forgot the legal stuff:

• Standard loopholes apply.
• In a tie, the oldest answer wins.
• Only numbers that your language can handle will be input.

### Test cases:

[] -> 1
[157] -> 158
[2,2] -> 3
[1,2,3] -> 3.3347175194727926933796024072445284958617665867248313464392241749...
[1,3,3,7] -> 7.0057883515880885509228954531888253495843595526169049621144740667...
[4,2] -> 4.3219280948873623478703194294893901758648313930245806120547563958...
[3,1,4] -> 4.2867991282182728445287736670302516740729127673749694153124324335...
[0,0,0,0] -> 1
[3.14,-2.718] -> 3.1646617321198729888284171456748891928500222706838871308414469486...


Output only needs to be as precise as your language can reasonably do.

• Please add a plain English description of the task along with a worked example or 2. Commented May 29, 2023 at 15:35
• I believe the interesting property is more general than that: f({n,n,...,n})=n+1 no matter how long the list is
– Leo
Commented May 30, 2023 at 0:12
• @Shaggy The mathematical notation used here is clear and inambiguous, so what's the problem? Commented May 31, 2023 at 8:28
• @Shaggy By the way, the plain english explanation will be much harder to understand. Commented May 31, 2023 at 9:11
• @Shaggy Abusing mathematical notation for tasks that can be simply described in plain English is probably a bad idea. But this one is a purely mathematical function which -- I believe -- can only be clearly defined with mathematical notation. At any rate, this debate cannot be settled here in the comments. So maybe it should be brought to meta? Commented May 31, 2023 at 9:44

# APL (Dyalog Unicode), 22 15 bytes

Anonymous tacit prefix function implementing $$f(x)=x_1\cdot\big[1=\lvert x\rvert\big]+\log_{\lvert x \rvert} \sum_{n=1}^{\lvert x\rvert}1^{\lvert x\rvert-n}\cdot\lvert x\rvert^{x_n}$$

(⊃×1=≢)+≢⍟1⊥≢*⊢


Try it online!

(

⊃ the first element

× times

1= whether (1) or not (0) one equals

≢ the length of the argument

)+ plus

≢⍟ the length-logarithm of

1⊥ the sum (lit. the base-one evaluation) of

≢ the length

* raised to the power of (each element in)

⊢ the argument

### Old solution: APL (Dyalog Extended), 22 bytes

Anonymous prefix lambda.

{0::1+⊃⍵⋄+/⍢((≢⍵)∘*)⍵}


Try it online!

{} "dfn"; argument is ⍵

0:: if any error happens:

1+ increment

⊃⍵ the first element of the argument

+/⍢()⍵ the sum of the argument, under the influence of:

()∘* raising to the power of:

≢⍵ the tally of elements (length of the argument)

• Wow answer within ~5 seconds gets blown away Commented May 29, 2023 at 15:22
• Nice! I had (⊃×1=≢)+≢⍟+⌿⍤(≢*⊢) Commented May 29, 2023 at 15:34
• Also, this is abusing that 1⍟1 is 1, which is, hmm, debatable Commented May 29, 2023 at 15:39
• @RubenVerg Using, my friend, using!
Commented May 29, 2023 at 15:40
• yay Iverson brackets Commented May 29, 2023 at 15:47

# JavaScript (ES7), 62 bytes

a=>a.map(v=>t+=n**v,t=0,n=a.length)&&(L=Math.log)(t)/L(n)||++a


Try it online!

### How?

Given the input list $$\a\$$ and its length $$\n\$$, we compute:

$$t=\sum_{i=0}^{n-1}n^{a_i}$$

We then try to compute $$\log_n(t)=log(t)/log(n)\$$.

If the result is NaN, we return ++a instead.

This will happen if:

• $$\n=0\$$ : we try to compute $$\\log(0)/\log(0)\$$
• $$\n=1\$$ : we try to compute $$\\log(1)/\log(1) = 0/0\$$

This gives the correct answer in both cases because:

• if $$\n=0\$$, we increment the empty array, which gives $$\1\$$.
• if $$\n=1\$$, there's a single atom $$\x\$$ in $$\a\$$ which is coerced to a number and incremented.

f[]=1
f[x]=1+x
f x|n<-sum$1<$x=logBase n$sum$map(n**)x


Try it online!

f[]=1
f[x]=1+x
f x|n<-sum$x>>[1]=logBase n$sum$map(n**)x  Try it online! # Haskell, 70 bytes f[]=1;f[x]=1+x;f x|let n=fromIntegral$length x=logBase n$sum$map(n**)x


Try it online!

• I had no idea that let guard syntax existed, but in any case you can replace it with a normal <-, and cut the fromIntegral out by computing the length from polymorphic Nums yourself for 56. Commented May 29, 2023 at 17:52
• 54 bytes
– xnor
Commented May 30, 2023 at 6:54

# R, 50 44 bytes

Edit: -1 byte thanks to @Dominic van Essen.

\(x,?=sum,l=?x|1)if(l<2,x?1,log(?l^x,l))


Attempt This Online!

• I got 49 bytes without looking at yours first, but it's pretty similar anyway... Commented May 30, 2023 at 8:00
• @DominicvanEssen Nice trick with the sum! That actuallly gave me an idea... Commented May 30, 2023 at 9:59

# Python NumPy, 81 bytes

lambda l:[sum(l)+1,w(l*(x:=w(l*0)))/x][x>0]
from numpy import*


Attempt This Online!

Takes a numpy array for input.

Test harness nicked from @AnttiP.

#### How?

Uses the almost-builtin: numpy.logaddexp.reduce Shame it's such a mouthful.

# Go, 160 bytes

import."math"
type F=float32
func f(N[]F)F{L,s:=F(len(N)),0.0
if L<1{return 1}
if L<2{return N[0]+1}
for i:=0;i<int(L);i++{s+=Pow(L,N[i])}
return Log(s)/Log(L)}


Attempt This Online!

# Excel, 57 bytes

=LET(x,A1#,y,ROWS(x),IF(y-1,LOG(SUM(y^x),y),IFNA(x+1,1)))


Input is vertical spilled range A1#.

# Python NumPy, 70 bytes

lambda l:math.log(sum(L**l),L)if(L:=len(l))>1else sum(l)+1
import math


Attempt This Online!

A rather dirty mix which expects a numpy array as input to take advantage of vectorised arithmetic but later uses the log from std lib math because it allows to specify the base.

Test harness from @AnttiP via @loopywalt.

# Python, 85 76 bytes

Essentially by @Albert.Lang

lambda l:math.log(sum(map(pow,[L:=len(l[2:])+2]*L,l+l+[0,0])),L)
import math


Attempt This Online!

• 76 I believe. Commented May 30, 2023 at 0:54

# Ruby, 57 53 bytes

->x{k=x.size;k>1?Math::log(x.sum{|c|k**c},k):x.sum+1}


Try it online!

# Vyxal, 24 Bytes

□L2<[?1+,]□L:ẋ□e∑∆l□L∆l/


Try it online! (List is given as input of numbers separated by newlines. This will not work if there is a trailing or leading newline in the input.)

If you want the output as a decimal instead of a fraction, use the ḋ flag.

If the length is 2 or above, it applies the general formula and implicitly prints the result. If the length is 1 or 0, it adds 1 to the number. With a list of length 1, this just increments the number and prints it. With a list of length 0, it increments it to get 1 and prints it, but then throws an error. Luckily, STDERR is ignored by default.

• Commented Jun 15, 2023 at 12:37
• Try it Online! for a functionally equivalent 11.75 byte answer to the 13 byte answer Commented Jun 15, 2023 at 12:43

# Charcoal, 27 bytes

Ｉ⎇Φθκ▷math.log⟦ΣＸＬθθＬθ⟧⊕↨θ¹


Attempt This Online!** Link is to verbose version of code. Explanation: Port of @AnttiP's Python answer.

   θ                        Input array
Φ                         Filtered where
κ                       Index is not zero
⎇                          If still not empty then
θ         Input array
Ｌ          Length
Ｘ           Vectorised raise to power
θ        Input array
Σ            Summed
▷math.log⟦       ⟧     Logarithm to base
θ      Input array
Ｌ       Length
θ  Otherwise input array
↨ ¹ Summed via "base 1" conversion*
⊕    Incremented
Ｉ                           Cast to string
Implicitly print


*Base 1 conversion returns 0 for an empty list, which Sum does not.
**I'm using ATO instead of TIO because the version of Charcoal on TIO incorrectly prints floats whose value is just below that of an integer.

# PARI/GP, 48 bytes

x->if(1<n=#x,log(vecsum(n^x))/log(n),n,x[1]+1,1)


Attempt This Online!

# 05AB1E, 17 15 bytes

g©ImO®.n®_+®i+


Explanation:

g©ImO®.n  # Otherwise portion of the formula:
g         #  Push the length of the (implicit) input-list
©        #  Store it in variable ® (without popping)
I       #  Push the input-list
m      #  Take the length to the power of each element
O     #  Sum them together
®.n  #  Take the base-® logarithm of that sum
®_+       # n=0 case of the formula:
®_        #  Push length ® again, and check whether it's 0 (1 if 0; 0 otherwise)
+       #  Add that to the value
®i+      # n=1 case of the formula:
®i        #  If length ® is 1:
+       #   Add the current value to the wrapped value of the input-list
      #   And unwrap it by popping and pushing its value to the stack
# (after which the result is output implicitly)


# Pyth, 15 bytes

.x.ls^lQQlQhe+Z


Try it online!

### Explanation

.x.ls^lQQlQhe+ZQ    # implicitly add Q
# implicitly assign Q = eval(input())
.x                  # try:
^lQQ           #   length(Q) to the power of each element of Q
s               #   sum all elements
.l     lQ         #   take the log base length Q
# except: (log will error if length is 0 or 1)
+ZQ    #   prepend 0 to Q
e       #   take the last element
h        #   and increment it


# Desmos, 686765 53 Bytes

Thanks to AidenChow for -12 Bytes!

l=a.length
f(a)=\{l=0,l=1:a[1]+1,\log_l(l^a.\total)\}


Now needs a keypress before it works
Without needing keypress (and visible) (66 65 Bytes):
-1 from AidenChow

f(a)=\left\{l=0,l=1:a[1]+1,log_l(l^a.total)\right\}withl=a.length


• You need to provide a Desmos graph link so that we can test your code. Commented May 31, 2023 at 16:50
• 53 bytes Commented Jun 1, 2023 at 18:13

# Prolog (SWI), 105 bytes

[H|T]*L*S:-T*L*N,S is N+L^H,!.
_*0.
A-K:-A=[B],K is B+1,!;length(A,L),L>1,A*L*S,K is log(S)/log(L),!;K=1.


Try it online!

Defines a predicate A-K which matches the ordered list A to its logarithmic incrementation K.

• Where did the surplus 1 come from after the 158? Commented Jun 28, 2023 at 6:54
• @Iamkindofalanguagedev Fixed. Commented Jun 28, 2023 at 23:40

# Scala, 95 bytes

Golfed version. Try it online!

l=>l match{case Nil=>1 case Seq(x)=>1+x case _=>{val n=l.size;log(l.map(pow(n,_)).sum)/log(n)}}


Ungolfed version. Try it online!

import scala.math._

object Main {
def f(l: List[Double]): Double = {
l match {
case Nil => 1.0
case List(x) => 1 + x
case _ => {
val n = l.count(_ => true)
log(l.map(pow(n, _)).sum) / log(n)
}
}
}

def main(args: Array[String]): Unit = {
println(f(List(3.0, 3.0)))
}
}


# Perl 5 List::Util, 43 bytes

sub{@_>1?log(sum map@_**\$_,@_)/log@_:1+pop}
`

Try it online!