TheAlgorithms/Go · error

arguments cannot be zero

Error message

arguments cannot be zero

What it means

A generic sentinel guard (math.ErrNonZeroArgsOnly) returned when Liouville's function is called with n == 0: the Liouville lambda is only defined over the positive integers, so a zero argument has no factorization to count prime factors from.

Source

Thrown at math/liouville.go:22

// For any positive integer n, define λ(n) as the sum of the primitive nth roots of unity.
// It has values in {−1, 1} depending on the factorization of n into prime factors:
//   λ(n) = +1 if n is a positive integer with an even number of prime factors.
//   λ(n) = −1 if n is a positive integer with an odd number of prime factors.
// wikipedia: https://en.wikipedia.org/wiki/Liouville_function
// time complexity: O(log n)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see liouville_test.go

package math

import (
	"errors"

	"github.com/TheAlgorithms/Go/math/prime"
)

var ErrNonZeroArgsOnly error = errors.New("arguments cannot be zero")

// Lambda is the liouville function
// This function returns λ(n) for given number
func LiouvilleLambda(n int) (int, error) {
	switch {
	case n < 0:
		return 0, ErrPosArgsOnly
	case n == 0:
		return 0, ErrNonZeroArgsOnly
	case len(prime.Factorize(int64(n)))%2 == 0:
		return 1, nil
	default:
		return -1, nil
	}
}

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Check n>0 before calling
  2. Return a domain default (e.g. 0) for zero input at the call site
  3. Document the positive-integer-only domain
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at math/liouville.go:22 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/cec4cf5e7ac24cd3. Report an issue: GitHub.