Questions tagged [math]
The challenge involves mathematics in some central way. Also consider using more specific tags, listed in the tag wiki info.
606
questions
44
votes
69
answers
4k
views
Fibonacci-orial
Definition
Fibonacci sequence F(n), on the positive integers, are defined as such:
...
43
votes
112
answers
6k
views
Divisibility test
Task
Given two strictly positive integers n and d as input, determine whether n is evenly divisible by d, i.e., if there exists an integer q such that n = qd.
You ...
41
votes
40
answers
13k
views
The rice and chess problem
An Indian legend tells the story of the alleged inventor of the chess game, who impressed the emperor of India with his game so much that he would get rewarded with anything asked.
The man said he ...
41
votes
6
answers
2k
views
World Big Dosa
You are a talented young chef who has just been offered the position of sous chef at the world's most prestigious Indian restaurant. You have little experience with preparing Indian cuisine, but you'...
40
votes
32
answers
5k
views
Is it a Proth number?
A Proth number, named after François Proth, is a number that can be expressed as
$$N = k \times 2^n + 1$$
Where \$k\$ is an odd positive integer and \$n\$ is a positive integer such that \$2^n > k\$...
38
votes
23
answers
4k
views
Natural Pi #0 - Rock
Goal
Create a program/function that takes an input N, check if N random pairs of integers are relatively prime, and returns <...
33
votes
39
answers
7k
views
Compute the Adler-32 checksum
Background
Adler-32 is a 32-bit checksum invented by Mark Adler in 1995 which is part of the widely used zlib library (also developed by Adler). Adler-32 is not as reliable as a 32-bit cyclic ...
27
votes
10
answers
3k
views
Help me carry my shopping bags
It was a warm summer evening...
when my stupid car decided to break down in the middle of the road on my way back from the supermarket. I pushed it to the sideline and decided to walk home. I opened ...
27
votes
8
answers
6k
views
The Bowlers-Golfers Fraction War
Introduction
Let's start by arranging all fractions from 0 to 1 in order of lowest denominator, then lowest numerator:
1/2, 1/3, 2/3, 1/4, 3/4, 1/5, 2/5, 3/5, 4/5, 1/6, 5/6, 1/7...
Note that ...
26
votes
20
answers
2k
views
Co-primality and the number pi
Introduction
Number theory is full of wonders, in the form of unexpected connections. Here's one of them.
Two integers are co-prime if they have no factors in common other than 1. Given a number \$N\$,...
25
votes
19
answers
4k
views
My Word can beat up your Word
PROBLEM
Given two words, find the winner in a digital root battle.
Define the digital root of a word this way:
Each letter of the alphabet is assigned a number: A = 1, B = 2, C = 3, ..., Z = 26
...
24
votes
11
answers
3k
views
Bijective function ℤ → ℤⁿ
It is trivially possible to create a bijective function from \$\mathbb{Z}\$ (the set of all integers) to \$\mathbb{Z}\$ (e.g. the identity function).
It is also possible to create a bijective ...
24
votes
16
answers
1k
views
Every Possible Cycle Length
A function (or program) which takes inputs and provides outputs can be said to have a cycle if calling the function on its own output repeatedly eventually reaches the original number. For instance, ...
24
votes
38
answers
4k
views
Reimplementing square root [duplicate]
Define a function, s, which takes a number and returns the square root.
No use of library functions, such as Java's Math.sqrt() or PHP's built in sqrt(), allowed.
24
votes
28
answers
5k
views
The Möbius Function
The Möbius function is an important number theoretic function.
Your submission should accept a positive integer \$n\$ and return the value of the Möbius function evaluated at \$n\$.
Definition
The ...
23
votes
6
answers
5k
views
Chinese Remainder Theorem
The Chinese Remainder Theorem tells us that we can always find a number that produces any required remainders under different prime moduli. Your goal is to write code to output such a number in ...
22
votes
13
answers
2k
views
Polynomialception
Given two polynomials f,g of arbitrary degree over the integers, your program/function should evaluate the first polynomial in the second polynomial. ...
22
votes
20
answers
6k
views
A penny saved is a penny
...counted!
You will pass your program a variable which represents a quantity of money in dollars and/or cents and an array of coin values. Your challenge is to output the number of possible ...
20
votes
13
answers
3k
views
Codegolf the permanent
The challenge is to write codegolf for the permanent of a matrix.
The permanent of an \$n\times n\$ matrix \$A = a_{i,j}\$) is defined as
$$\text{perm}(A) = \sum_{\sigma \in S_n} \prod^n_{i=1} a_{i,\...
19
votes
12
answers
3k
views
Decompose a permutation into cycles
There is a well-known theorem that any permutation can be decomposed into a set of cycles. Your job is to write the shortest possible program to do so.
Input:
Two lines. The first contains a ...
19
votes
29
answers
3k
views
Do Matrix Multiplication!
In mathematics, matrix multiplication or the matrix product is a binary operation that produces a matrix from two matrices. The definition is motivated by linear equations and linear transformations ...
19
votes
31
answers
8k
views
Calculate pi to 5 decimals [closed]
This comes from http://programmers.blogoverflow.com/2012/08/20-controversial-programming-opinions/
"Given that Pi can be estimated using the function \$4 \times (1 - 1/3 + 1/5 - 1/7 + \cdots)\$ ...
18
votes
28
answers
2k
views
Given a string, calculate the number of the column it corresponds to
In Excel, the columns range from A-Z, AA,AB,AZ,BA,..,BZ and so on. They actually each stand for numbers, but rather are encoded as alphabet strings.
In this ...
18
votes
12
answers
880
views
Compute modular inverse
Given two positive numbers \$x\$ and \$n\$ with \$x<2^n\$, write the shortest possible function to compute \$x^{-1} \mod 2^n\$. In other words, find \$y\$ such that \$xy=1 \mod 2^n\$.
Your ...
18
votes
6
answers
3k
views
Electron Configurations
Electron Configurations
Your mission is to accept an element's atomic number as input, and output its electron configuration (e.g. 2,8,8,2 for calcium).
Input
An ...
17
votes
18
answers
2k
views
Computing a specific coefficient in a product of polynomials
Generator functions
This gives the context for why this challenge came to life. Feel free to ignore.
Generator functions are a nice way of encoding the solution to a problem of combinatorics. You ...
17
votes
34
answers
6k
views
The program that will find the next prime number
Intro:
You accidentally corrupted the flow of time with a device you made for fun, that turned out to be a time machine. As a result, you got pushed to the far future. You realized that computing, ...
16
votes
32
answers
2k
views
Plot a centered circle
Intro
Given radius \$r\$, draw a circle in the center of the screen.
Sandbox.
The Challenge
Here is a simple challenge.
Plot a circle using the formula \$x^2+y^2=r^2\$, or any other formula that will ...
16
votes
5
answers
2k
views
Polynomial Long Division
Implement polynomial long division, an algorithm that divides two polynomials and gets the quotient and remainder:
(12x^3 - 5x^2 + 3x - 1) / (x^2 - 5) = 12x - 5 R 63x - 26
In your programs, you will ...
15
votes
4
answers
2k
views
Solve a 2x2 Eigensystem
For those with a little linear algebra background, the challenge is as simple as this: determine the eigenvalues and eigenvectors of a given complex 2x2 matrix. You may skip ahead to The Challenge for ...
15
votes
3
answers
1k
views
Find the shortest Golomb rulers
Golomb rulers are sets of non-negative integers such that no two pairs of integers in the set are the same distance apart.
For example, [0, 1, 4, 6] is a Golomb ...
14
votes
8
answers
3k
views
Determining the continued fractions of square roots
The continued fraction of a number \$n\$ is a fraction of the following form:
$$a_0 + \cfrac 1 {a_1 + \cfrac 1 {a_2 + \cfrac 1 {a_3 + \cfrac 1 {a_4 + \ddots}}}}$$
which converges to \$n\$.
The ...
13
votes
9
answers
1k
views
Find the repetend of the decimal representation!
In this challenge 2 years ago, we found the period of a unit fraction (1/n where n is a natural number).
Now, your task is to write a program/function to find the ...
13
votes
14
answers
1k
views
Calculate the vector component
Challenge
Assume two vectors \$\mathbf{a} = (a_1,a_2,\cdots,a_n)\$ and \$\mathbf{b} = (b_1,b_2,\cdots,b_n)\$ are given in an \$n\$-dimensional space, where at least one of \$b_1,\cdots,b_n\$ is ...
12
votes
12
answers
2k
views
Verify Magic Square
A magic square is a square array of numbers with side n consisting of the distinct positive integers 1, 2, ..., n² arranged such that the sum of the n numbers in any horizontal, vertical, or main ...
12
votes
6
answers
888
views
Bijection between binary strings and pairs thereof
Input: Either one or two strings consisting of '0's and '1's. If there are 2, they are separated by a space. All strings are of ...
11
votes
6
answers
1k
views
Transmit Pi... precisely
Following on from Monte Carlo estimator of Pi this challenge is to produce the shortest code for the constant Pi. Except here your code must output consecutive digits of pi forever.
This is code ...
11
votes
5
answers
955
views
Raise to the Power
Challenge
Challenge is writing a program that takes a positive numbers a and a nonzero number b and outputs ...
11
votes
3
answers
1k
views
Math in manhattan
I define the following operators:
Manhattan Addition a +M b, for single-digit numbers, is the result of concatenating b onto a. So, a +M b = 10a + b. Therefore, the general operator +M is defined as ...
11
votes
10
answers
2k
views
Fibonacci domino tiling
There's classic combinatorial result that the number of ways to tile a 2*n strip by 1*2 dominoes is the nth Fibonacci number. ...
10
votes
20
answers
973
views
Calculate probability of getting half as many heads as coin tosses.
Write a program that, given a small positive even integer from standard input, calculates the probability that flipping that many coins will result in half as many heads.
For example, given 2 coins ...
9
votes
16
answers
4k
views
Write a mathematical function whose result is "hello world" [closed]
Write a mathematical function that results in an integer representation of "hello world"
For example "hello world" in hex is 68656c6c6f20776f726c64 so the goal ...
8
votes
3
answers
389
views
Rational Polynomial Interpolation
Explanation
In this task you'll be given a set of N points (x1,y1),…,(xN,yN) with distinct xi values and your task is to interpolate a polynomial through these points. If you know what Lagrange ...
6
votes
9
answers
882
views
Code Golf: Number of paths!
Challenge
Consider a square matrix of order N(N
rows and N columns). At each step you
can move one step to the right or one
step to the top. How many
possibilities are to reach (N,N) from
...
5
votes
4
answers
2k
views
Amicable number calculator [closed]
Write a program that calculates amicable numbers, starting from [0,0], intil the maximum value for an integer is reached in the language you chose to write it in.
Amicable numbers are two different ...
3
votes
7
answers
861
views
Resolve quadratic equation
Challenge:
Write the smallest program (in characters) that resolves quadratic equations
i.e. ax² + bx + c = 0
Rules:
Given 3 numbers in R comma-delimited on ...
-7
votes
46
answers
1k
views
Quartic Summation
Guidelines
Task
Given two non-negative integers, find the sum of both numbers... to the power of 4.
Examples
2, 3 -> 97 (2^4 + 3^4 = 97)
...
194
votes
62
answers
30k
views
Mathematics is fact. Programming is not
In mathematics an exclamation mark ! often means factorial and it comes after the argument.
In programming an exclamation mark ! ...
104
votes
30
answers
7k
views
Does the code terminate?
This is a code golf challenge I thought of with a mathematical bent. The challenge is to write the shortest code possible such that it is an open question whether or not the code terminates. An ...
103
votes
39
answers
20k
views
Mandelbrot image in every language
I always used a Mandelbrot image as the 'graphical' version of Hello World in any graphical application I got my hands on. Now it's your guys' turn.
Language must be capable of graphical output or ...