euler.helper

&

function composition

abundant-sum?

(abundant-sum? n)

abundant-sum? :: Int -> Boolean Can n be expressed as the sum of two abundant numbers?

abundant?

(abundant? n)

abundant? :: Int -> Bool Checks whether the sum of the factors of n (excluding n) is greater than n.

abundants

abundants :: Int All abundant numbers.

amicable?

(amicable? a)

amicable? :: Int -> Bool Is a an amicable number?

benchmark

(benchmark f N x)

Map f over the range of N x times.

collatz

(collatz n)

collatz :: Int Lazy seq of Collatz sequence of n.

combinations

(combinations amount [car & cdr :as coins])

combinations :: Int, Int -> Int Returns the number of combinations that can be computed from the list of numbers.

count-divisors

(count-divisors n)

count-divisors :: Int -> Int Calculates the number of divisors of n (including 1 and n itself).

digits

(digits n)

digits :: Int -> Int Converts a number n into a list of its digits.

exp-by-square

(exp-by-square x n)

factor-any

(factor-any divisors argument)

factor-any :: Int -> Int -> Bool: Predicate that tests whether its argument can be evenly divided by any of the divisors.

factor?

factor? :: dividend:Int, divisor:Int -> Bool: Predicate that tests whether the divisor evenly divides the dividend.

factorial

(factorial n)

Int -> Int Multiplies all natural numbers from 1 to n+1.

factorials

facts :: Int Lazy, infinite sequence of all factorial numbers.

factors

(factors n)(factors n start)

factors :: Int -> Int Returns all numbers that evenly divide n.

fibs

fibs :: Int Lazy sequence of all Fibonacci numbers. Using BigIntegers.

Background: A Fibonacci number (exept 0 and 1) is recursively defined as the sum of its two predecessing Fibonacci numbers:

  • base case: fib(n <= 1) = n

  • rec case: fib(n > 1) = fib(n - 1) + fib(n - 2)

get-primes

(get-primes n)(get-primes n [car & cdr :as ps] acc)

get-primes :: Int -> Int Get the prime divisors of n.

lattice-paths

(lattice-paths n)

lattice-paths :: Int -> Int The central binomial coefficients, Binomial2n, n or (2n)!/(n!)^2.

least-common-multiple

(least-common-multiple input)

least-common-multiple :: Int -> Int Computes the smallest number divisible by all of the given input numbers. See http://en.wikipedia.org/wiki/Least_common_multiple.

max-prime

(max-prime n [car & cdr :as ps])

max-prime :: Int, Int -> Int Given a number n and a list of prime numbers ps, returns the largest prime factor of n.

memo-collatz

(memo-collatz c n)

memo-collatz :: Int Memoized collatz sequence using an atom.

narcissistic?

(narcissistic? n exp)

narcissistic? :: Int, Int -> Bool Valid numbers can be written as the sum of its nth powers. Exp.: 1634 = 1^4 + 6^4 + 3^4 + 4^4.

next-collatz

(next-collatz n)

next-collatz :: Int -> Int Computes the next number of the collatz sequence.

p

partial application

palindrome?

(palindrome? n)

palindrome? :: Int -> Bool Checks whether the given number n is a palindrome. Uses reversed string comparison

pandigital?

(pandigital? n)

Makes use of all the digits 1 to n exactly once.

parse-grid

(parse-grid grid-str dimension)

parse-grid :: String -> Int Converts a string into a dimension x dimension vector of integers.

permutations

(permutations s)

phi

phi :: Double The Golden Ratio - (1+sqrt5)/2.

prime-factors

(prime-factors n)(prime-factors number factors ps)

** prime-factors :: Int -> Int** Given a number, returns the list of prime factors of n. Example: (prime-factors 12) => (2 2 3)

prime-fast?

(prime-fast? n)

prime?

(prime? n)

prime? :: Int -> Bool Checks whether a given number n is a prime number.

primes

primes :: Int Lazy stream of prime numbers, generated by the sieve of Eratosthenes algorithm. Internally stores in a hastable the next composite number corresponding to the prime number. Taken from http://voidmainargs.blogspot.de/2010/12/ lazy-sieve-eratosthenes-in-scala-and.html

product

(product size len grid)

Inspired by Chouser

rotations

(rotations x)

Returns a lazy seq of all rotations of a seq. Taken from clojure.contrib(https://clojure.github.io/clojure-contrib/ seq-api.html#clojure.contrib.seq/rotations)

singles

singles :: Int The literals of the numbers 1,2,..19

sum-of-factors

(sum-of-factors n)

sum-of-factors :: Int -> Int The sum of all proper divisors, excluding n.

tens

tens :: Int The literals of the numbers 20,30,…90

to-words

(to-words n)

Converts a number (up to 1000) into its string representation, omitting spaces. Example: (to-words 115) => ‘onehundredandfifteen’.

triangle?

(triangle? n)

triangle? :: Int -> Bool Checks whether a given number n is a triangle number.

triangles

triangle :: Int Lazy seq of all triangle numbers. A triangle number is generated by adding the natural numbers up to n.

truncate

(truncate number digits)

truncate :: Int, Int -> Int Truncates the given number up to the first n digits.