Questions tagged [math]

The challenge involves mathematics in some central way. Also consider using more specific tags, listed in the tag wiki info.

Filter by
Sorted by
Tagged with
42 votes
64 answers
9k views

Generate Pascal's triangle

Pascal's triangle is generated by starting with a 1 on the first row. On subsequent rows, the number is determined by the sum of the two numbers directly above it to the left and right. To ...
user avatar
  • 7,672
84 votes
127 answers
12k views

Collatz Conjecture (OEIS A006577)

This is the Collatz Conjecture (OEIS A006577): Start with an integer n > 1. Repeat the following steps: If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. It is proven that ...
user avatar
  • 70.6k
15 votes
15 answers
2k views

Golf the Subset-Sum Problem

Task Given a list of space-delimited integers as input, output all unique non-empty subsets of these numbers that each subset sums to 0. Test Case Input: ...
user avatar
  • 2,172
35 votes
18 answers
11k views

Find the nth decimal of pi

There are already 30 challenges dedicated to pi but not a single one asks you to find the nth decimal, so... Challenge For any integer in the range of ...
user avatar
43 votes
74 answers
7k views

Greatest Common Divisor

Your task is to compute the greatest common divisor (GCD) of two given integers in as few bytes of code as possible. You may write a program or function, taking input and returning output via any of ...
user avatar
30 votes
30 answers
3k views

Josephus problem (counting out)

The challenge Write a function that takes two positive integers \$n\$ and \$k\$ as arguments and returns the number of the last person remaining out of \$n\$ after counting out each \$k\$-th person. ...
user avatar
  • 23.4k
80 votes
192 answers
15k views

Sum of all integers from 1 to n

I'm honestly surprised that this hasn't been done already. If you can find an existing thread, by all means mark this as a duplicate or let me know. Input Your input is in the form of any positive ...
user avatar
  • 1,259
61 votes
42 answers
13k views

Find the Smoothest Number

Your challenge is to find the smoothest number over a given range. In other words, find the number whose greatest prime factor is the smallest. A smooth number is one whose largest prime factor is ...
user avatar
  • 41.4k
50 votes
75 answers
7k views

It's Hip to be Square

Challenge So, um, it seems that, while we have plenty of challenges that work with square numbers or numbers of other shapes, we don't have one that simply asks: Given an integer ...
user avatar
  • 35.5k
27 votes
17 answers
3k views

Compute the multinomial coefficient

Time for another easy challenge in which all can participate! The multinomial theorem states: The expression in parentheses is the multinomial coefficient, defined as: Allowing the terms ki to ...
user avatar
  • 4,087
391 votes
156 answers
272k views

Write a program that makes 2 + 2 = 5 [closed]

Write a program that seemingly adds the numbers 2 and 2 and outputs 5. This is an underhanded contest. Your program cannot output any errors. Watch out for memory holes! Input is optional. ...
user avatar
  • 11.2k
46 votes
41 answers
18k views

Draw A Sierpinski Triangle

The Sierpinsky Triangle is a fractal created by taking a triangle, decreasing the height and width by 1/2, creating 3 copies of the resulting triangle, and place them such each triangle touches the ...
user avatar
  • 949
40 votes
15 answers
5k views

Shortest terminating program whose output size exceeds Graham's number

Write the shortest possible program (length measured in bytes) satisfying the following requirements: no input output is to stdout execution eventually terminates total number of output bytes exceeds ...
user avatar
  • 3,042
56 votes
76 answers
8k views

Test a number for narcissism

A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits. For example, take \$153\$ (3 digits): \$1^3 + 5^3 + 3^3 = 153\$ \$1634\$: \$1^...
user avatar
  • 2,498
35 votes
47 answers
3k views

Least Common Multiple

The least common multiple of a set of positive integers A is the smallest postive integer B such that, for each ...
user avatar
25 votes
8 answers
2k views

All your bijective base are belong to us

Background A bijective base b numeration, where b is a positive integer, is a bijective positional notation that makes use of b symbols with associated values of 1 to b. Unlike its non-bijective ...
user avatar
  • 207k
23 votes
4 answers
3k views

Four fours puzzle

The Four fours puzzle is a popular recreational mathematical puzzle that involves using exactly four 4s (and no other number) and a defined set of operations to reach every number from 0 to a given ...
user avatar
  • 3,354
32 votes
47 answers
6k views

Implement hyperexponentiation/tetration without the use of '^' [closed]

The Challenge Implement tetration (aka Power Tower or Hyperexponentiation) with the least amount of characters. The Conditions Don't use the 'power' operator or its equivalents (such as ...
user avatar
  • 931
29 votes
5 answers
2k views

Advanced Calculator

You must write a program that evaluates a string that would be entered into an advanced calculator. The program must accept input using stdin and output the correct answer. For languages that do no ...
user avatar
41 votes
28 answers
11k views

XOR multiplication

You goal is to implement the operation of XOR (carryless) multiplication, defined below, in as few bytes as possible. If we think of bitwise XOR (^) as binary ...
user avatar
  • 138k
60 votes
41 answers
9k views

Print the N-bonacci sequence

This isn't very widely known, but what we call the Fibonacci sequence, AKA 1, 1, 2, 3, 5, 8, 13, 21, 34... is actually called the Duonacci sequence. This is ...
user avatar
  • 57.4k
24 votes
11 answers
4k views

Generate a Kolakoski sequence [duplicate]

Definition1 A Kolakoski sequence is a self-describing infinite sequence {kn} of alternating blocks of 1's and 2's, given by the following rules: k0 = 1 kn = the length of the (n+1)'th ...
user avatar
  • 2,289
34 votes
18 answers
8k views

Balanced Ternary Converter

Credits for the challenge idea go to @AndrewPiliser. His original proposal in the sandbox was abandoned and since he has not been active here for several months, I have taken over the challenge. ...
user avatar
12 votes
5 answers
1k views

Polynomial Interpolation

Write a program that performs Polynomial Interpolation using true arbitrary precision rational numbers. The input looks like this: f(1) = 2/3 f(2) = 4/5 f(3) = 6/7 ... You may assume that there's ...
user avatar
  • 9,970
11 votes
25 answers
3k views

Cartesian product of a list with itself n times

When given a a list of values and a positive integer n, your code should output the cartesian product of the list with itself n ...
user avatar
  • 437
55 votes
9 answers
11k views

Golf a number bigger than TREE(3)

The function TREE(k) gives the length of the longest sequence of trees T1, T2, ... where each vertex is labelled with one of k colours, the tree Ti has at most i vertices, and no tree is a minor of ...
user avatar
  • 7,079
44 votes
43 answers
10k views

Reverse Polish notation

You must evaluate a string written in Reverse Polish notation and output the result. The program must accept an input and return the output. For programming languages that do not have functions to ...
user avatar
7 votes
17 answers
3k views

Multiply without multiply [closed]

Write the fastest (best big-O) and smallest multiplication algorithm for positive integers, without using multiplication operators. You are only allowed addition, subtraction, logical functions (AND, ...
user avatar
  • 3,262
42 votes
67 answers
8k views

I reverse the source code, you negate the input!

Blatant rip-off of a rip-off. Go upvote those! Your task, if you wish to accept it, is to write a program/function that outputs/returns its integer input/argument. The tricky part is that if I ...
user avatar
  • 28k
36 votes
28 answers
5k views

Find the number in the Champernowne constant

Introduction In base 10, the Champernowne constant is defined by concatenating representations of successive integers. In base 10: 0.1234567891011121314151617... ...
user avatar
  • 43.4k
19 votes
14 answers
2k views

Discrete Convolution or Polynomial Multiplication

Given two non empty lists of integers, your submission should calculate and return the discrete convolution of the two. Interestingly, if you consider the list elements as coefficients of polynomials, ...
user avatar
  • 43.1k
50 votes
47 answers
4k views

Count sums of two squares

Given a non-negative number n, output the number of ways to express n as the sum of two squares of integers ...
user avatar
  • 138k
22 votes
18 answers
2k views

Calculate the partitions of N

Your challenge is simple: GIven an integer N, ouput every list of positive integers that sums to N. For example, if the input was 5, you should output ...
user avatar
  • 57.4k
11 votes
11 answers
1k views

Minimum perimeter of an area [duplicate]

Just a simple code golf function for fun, intentionally left open with few rules to see what creativity comes up. Input: An integer representing the area of a rectangle. Output: Two integers ...
user avatar
  • 8,570
326 votes
55 answers
109k views

Tweetable Mathematical Art [closed]

Integer math can generate amazing patterns when laid out over a grid. Even the most basic functions can yield stunningly elaborate designs! Your challenge Write 3 Tweetable (meaning 140 characters ...
user avatar
107 votes
387 answers
19k views

One OEIS after another

As of 13/03/2018 16:45 UTC, the winner is answer #345, by Khuldraeseth na'Barya. This means the contest is officially over, but feel free to continue posting answers, just so long as they follow the ...
user avatar
47 votes
68 answers
6k views

An abundance of integers!

An Abundant number is any number where the sum of its proper divisors is greater than the original number. For example, the proper divisors of 12 are: ...
user avatar
  • 57.4k
83 votes
183 answers
9k views

Output the sign

Given a number N, output the sign of N: If N is positive, output 1 If N is negative, output -1 If N is 0, output 0 N will be an integer within the representable range of integers in your chosen ...
user avatar
40 votes
61 answers
6k views

Zeroes at the end of a factorial

Write a program or function that finds the number of zeroes at the end of n! in base 10, where n is an input number (in any ...
user avatar
  • 7,219
35 votes
38 answers
4k views

Determinant of an Integer Matrix

Given a square integer matrix as input, output the determinant of the matrix. Rules You may assume that all elements in the matrix, the determinant of the matrix, and the total number of elements in ...
user avatar
51 votes
47 answers
8k views

Coprimes up to N

Given a number n >= 2, output all the positive integers less than n where gcd(n, k) == 1 (...
user avatar
  • 7,684
11 votes
1 answer
2k views

Generate a number by using a given list of numbers and arithmetic operators

You are given a list of numbers L = [17, 5, 9, 17, 59, 14], a bag of operators O = {+:7, -:3, *:5, /:1} and a number ...
user avatar
30 votes
66 answers
5k views

Count the divisors of a number

Introduction This is a very simple challenge: simply count the divisors of a number. We've had a similar but more complicated challenge before, but I'm intending this one to be entry-level. The ...
user avatar
  • 2,202
25 votes
11 answers
3k views

Generate a pair of integers from a non-negative one

You should write a program or function which takes a non-negative integer N as input and outputs or returns two integers (negative, zero or positive) ...
user avatar
  • 20.6k
20 votes
1 answer
921 views

Factor a polynomial over a finite field or the integers

Without using any built-in factoring/polynomial functions, factor a polynomial completely into irreducibles over the integers or a finite field. Input Your program/function will receive some prime (or ...
user avatar
  • 20.9k
19 votes
21 answers
2k views

Length of String Using Set Theory

From Wikipedia Set-theoretic definition of natural numbers The set N of natural numbers is defined as the smallest set containing 0 and closed under the successor function S defined by S(n) = n ∪ ...
user avatar
  • 365
19 votes
2 answers
862 views

Dames, do some math!

The order of operations, PEMDAS, is a basic rule in mathematics telling us which order operations should be performed: "Parentheses, Exponents, Multiplication and Division, and Addition and ...
user avatar
14 votes
9 answers
2k views

What is the average of n, the closest prime to n, the square of n and the closest Fibonacci number to n?

This is a math problem which takes quite many things into question, making it rather challenging, and as you might have guessed, it's a code golf, so it should be as short as possible as well. The ...
user avatar
  • 471
29 votes
26 answers
1k views

Natural construction

The natural numbers including 0 are formally defined as sets, in the following way: Number 0 is defined as the empty set, {} For n ≥ 0, number n+1 is defined as n ∪ {n}. As a consequence, n = {0, 1, ...
user avatar
  • 100k
165 votes
5 answers
17k views

Hyperprogramming: N+N, N×N, N^N all in one

Write a program that takes in a number N from 1 to 9 inclusive. In its native form your program should output N+N. E.g. output 2 if N is ...
user avatar

1
2 3 4 5
12