19
\$\begingroup\$

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.

\$\endgroup\$
14
  • 4
    \$\begingroup\$ Please add a plain English description of the task along with a worked example or 2. \$\endgroup\$
    – Shaggy
    May 29, 2023 at 15:35
  • 7
    \$\begingroup\$ I believe the interesting property is more general than that: f({n,n,...,n})=n+1 no matter how long the list is \$\endgroup\$
    – Leo
    May 30, 2023 at 0:12
  • 3
    \$\begingroup\$ @Shaggy The mathematical notation used here is clear and inambiguous, so what's the problem? \$\endgroup\$
    – xigoi
    May 31, 2023 at 8:28
  • 5
    \$\begingroup\$ @Shaggy By the way, the plain english explanation will be much harder to understand. \$\endgroup\$ May 31, 2023 at 9:11
  • 3
    \$\begingroup\$ @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? \$\endgroup\$
    – Arnauld
    May 31, 2023 at 9:44

19 Answers 19

7
\$\begingroup\$

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)

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

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.
\$\endgroup\$
4
\$\begingroup\$

Haskell, 54 bytes

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

Try it online!

Haskell, 56 bytes

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ May 29, 2023 at 17:52
  • 3
    \$\begingroup\$ 54 bytes \$\endgroup\$
    – xnor
    May 30, 2023 at 6:54
3
\$\begingroup\$

Python NumPy, 81 bytes

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

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.

\$\endgroup\$
3
\$\begingroup\$

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!

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

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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 76 I believe. \$\endgroup\$ May 30, 2023 at 0:54
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Ruby, 57 53 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Vyxal, 24 Bytes

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

My first Vyxal answer!

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.

\$\endgroup\$
2
1
\$\begingroup\$

Charcoal, 27 bytes

I⎇Φθκ▷math.log⟦ΣXLθθLθ⟧⊕↨θ¹

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
                 L          Length
                X           Vectorised raise to power
                   θ        Input array
               Σ            Summed
     ▷math.log⟦       ⟧     Logarithm to base
                     θ      Input array
                    L       Length
                         θ  Otherwise input array
                        ↨ ¹ Summed via "base 1" conversion*
                       ⊕    Incremented
I                           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.

\$\endgroup\$
1
\$\begingroup\$

PARI/GP, 48 bytes

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

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 17 15 bytes

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

Try it online or verify all test cases.

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)
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Desmos, 68 67 65 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

Link

\$\endgroup\$
2
  • \$\begingroup\$ You need to provide a Desmos graph link so that we can test your code. \$\endgroup\$
    – Aiden Chow
    May 31, 2023 at 16:50
  • \$\begingroup\$ 53 bytes \$\endgroup\$
    – Aiden Chow
    Jun 1, 2023 at 18:13
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Where did the surplus 1 come from after the 158? \$\endgroup\$ Jun 28, 2023 at 6:54
  • \$\begingroup\$ @Iamkindofalanguagedev Fixed. \$\endgroup\$ Jun 28, 2023 at 23:40
0
\$\begingroup\$

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)))
  }
}
\$\endgroup\$
0
\$\begingroup\$

Perl 5 List::Util, 43 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.