As of May 31, 2023, we have updated our Code of Conduct.

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
10 votes
40 answers
2k views

Output endless powers of 2 [duplicate]

In any programming language, make a program that outputs infinite subsequent powers of two, starting at any power of two with finite integer exponent. There does not need to be a seperator between ...
Dadsdy's user avatar
  • 339
16 votes
17 answers
1k views

Logarithmic Incrementation

In this challenge you will write a function that takes a list (ordered set) containing real numbers (except for the empty list) and calculates $$f(x)=\begin{cases}1 & \text{if } |x|=0 \\ x_1+1 &...
Hippopotomonstrosesquipedalian's user avatar
21 votes
6 answers
831 views

How many sorting networks?

Below on the left is a picture of a sorting network that can sort 4 inputs. On the right you can see it sorting the input 3,2,4,1. A sorting network of size ...
AnttiP's user avatar
  • 7,355
14 votes
12 answers
1k views

Resultant of two polynomials

The resultant of two polynomials is a polynomial in their coefficients that is zero if and only if \$p\$ and \$q\$ have a common root. It is a useful tool for eliminating variables from systems of ...
alephalpha's user avatar
  • 41.9k
14 votes
8 answers
979 views

Euclidean distance on projective plane

Motivated by this challenge Background Let we have a square sheet of flexible material. Roughly speaking, we may close it on itself four ways: Here the color marks the edges that connect and the ...
lesobrod's user avatar
  • 3,031
20 votes
9 answers
3k views

How powerful must this e approximation be?

This challenge was inspired by this non-challenge about the natural logarithm base \$e\$ and the following pandigital approximation to \$e\$ appearing on a Math Magic page: $$\left|(1+9^{-4^{7×6}})^{3^...
Parcly Taxel's user avatar
  • 3,637
9 votes
3 answers
291 views

Representing a number as an unordered list of smaller numbers

Suppose we want to encode a large integer \$x\$ as a list of words in such a way that the decoder can recover \$x\$ regardless of the order in which the words are received. Using lists of length \$k\$ ...
Karl's user avatar
  • 501
41 votes
17 answers
3k views

Compare Two Fractions With ASCII Art

Challenge Write a program or function that takes in 4 non-negative integers, A, B, C, and D, that represent two fractions, A/B and C/D, where B and D are non-zero and A <= B and C <= D. Output ...
blaketyro's user avatar
  • 719
21 votes
13 answers
2k views

Minkowski's ?(x) for rational x

Here is Minkowski's question mark function: It is a strictly increasing and continuous function from the reals to themselves that, among other unusual properties, maps rational numbers to dyadic ...
Parcly Taxel's user avatar
  • 3,637
10 votes
5 answers
235 views

Calculate the Smith normal form of an integer matrix

Given an \$m \times n\$ matrix of integers A, there exist a \$m \times m\$ matrix P, an \$m \times n\$ matrix D, and an \$n \times n\$ matrix Q such that: \$A = P D Q\$. P and Q are unimodular ...
Daniel Schepler's user avatar
22 votes
20 answers
2k views

Calculate Euclidean distance on a torus

Euclidean distance between two lattice points \$(x_1, y_1)\$ and \$(x_2, y_2)\$ on a plane is: \$\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}\$. Imagine now a lattice ...
anatolyg's user avatar
  • 13.1k
16 votes
15 answers
1k views

Find Index of Rational Number in Calkin-Wilf Sequence

Related From Wikipedia: In number theory, the Calkin–Wilf tree is a tree in which the vertices correspond one-to-one to the positive rational numbers. The tree is rooted at the number \$1\$, and any ...
97.100.97.109's user avatar
15 votes
13 answers
1k views

Hankel transform of an integer sequence

A Hankel matrix is a square matrix in which each ascending skew-diagonal from left to right is constant, e.g.: $$\begin{bmatrix} a & b & c & d \\ b & c & d & e \\ c & d &...
alephalpha's user avatar
  • 41.9k
24 votes
5 answers
2k views

Add two real numbers ... probably

The problem statement here is pretty simple, take two real numbers on the range [0,1) as input and output their sum, with probability 1. The catch here is that there are a lot of real numbers. There ...
Wheat Wizard's user avatar
  • 94.5k
25 votes
37 answers
2k views

Alternating factorial

The alternating factorial is an alternating sum of decreasing factorials. For example, we could calculate the alternating factorial of 4 as follows: First, calculate the factorials from 4 down to 1: ...
chunes's user avatar
  • 18.1k
17 votes
32 answers
2k views

The Jaccard Index

The Jaccard index / similarity coefficient, also known as the Tanimoto index / coefficient, is a statistic used for gauging the similarity and diversity of finite sample sets. It was developed by ...
solid.py's user avatar
  • 1,527
21 votes
29 answers
2k views

Cosine similarity of two vectors

The cosine similarity of two vectors \$A\$ and \$B\$ is defined using their dot product and magnitude as: \$\frac{A\cdot B}{\|A\|\|B\|}\$ Or in other terms \$\frac{\sum_{i=1}^nA_iB_i}{\sqrt{\sum_{i=1}^...
emirps's user avatar
  • 1,696
12 votes
5 answers
360 views

Enumeration of free polyominoes

A polyomino with \$n\$ cells is a shape consisting of \$n\$ equal squares connected edge to edge. No free polyomino is the rotation, translation or reflection (or a combination of these ...
math scat's user avatar
  • 7,607
10 votes
5 answers
583 views

Record Least Uncommon Multiple Counts

The Greatest Common Divisor, or gcd, of two positive integers \$x\$ and \$y\$ is the largest positive integer that divides both \$x\$ and \$y\$. The Least Common Multiple, or lcm, of two positive ...
Kip the Malamute's user avatar
16 votes
8 answers
2k views

We're gonna need a bigger podium!

If \$R\$ runners were to run a race, in how many orders could they finish such that exactly \$T\$ runners tie? Challenge Given a positive integer \$R\$ and a non-negative integer \$0\leq T\leq {R}\$ ...
Jonathan Allan's user avatar
16 votes
6 answers
1k views

Detect round trips on a hyperbolic grid

You're driving a car in an infinite city whose blocks are pentagons arranged in the order-4 pentagonal tiling. At each step, you proceed to the next intersection and choose whether to continue left, ...
Karl's user avatar
  • 501
12 votes
2 answers
273 views

Classify a surface from its fundamental polygon

This question is an extension of Who's that Polygon? to arbitrary numbers of sides. A fundamental polygon for a surface is an polygon with a prescribed pairing for all its \$2n\$ sides, each ...
Parcly Taxel's user avatar
  • 3,637
4 votes
1 answer
377 views

Generate all possible equations with 10 characters

Nerdle is a Wordle variant, in which instead of words, the answers are equations. Each equation entered in the game must be a valid one. Examples: 13³-1=2196, ...
ordptt's user avatar
  • 151
5 votes
2 answers
224 views

Canonical form of a cubic Bézier curve

On Pomax's Primer on Bézier Curves this "fairly funky image" appears: This is related to the fact that every cubic Bézier curve can be put in a "canonical form" by an affine ...
Parcly Taxel's user avatar
  • 3,637
3 votes
0 answers
125 views

4D rotation matrix to quaternions

It is well-known that a 3D rotation can always be represented by a quaternion. It is less well-known that a 4D rotation can always be represented by two quaternions, sending a point \$p=(a,b,c,d)^T\$ ...
Parcly Taxel's user avatar
  • 3,637
12 votes
6 answers
601 views

Number Clusters

Your task is to create a program or function, that when given an input list of nonnegative integers of length \$l \ge 2\$ and a nonnegative integer \$c\$ where \$2 \le c \le l\$, group the list into \$...
Yousername's user avatar
  • 3,100
9 votes
5 answers
436 views

3D rotation matrix to quaternion

There are multiple ways to represent a 3D rotation. The most intuitive way is the rotation matrix – $$A=\begin{bmatrix}A_{11}&A_{12}&A_{13}\\A_{21}&A_{22}&A_{23}\\A_{31}&A_{32}&...
Parcly Taxel's user avatar
  • 3,637
20 votes
9 answers
1k views

Split some points

proposed by @Adám in chat Given an even number of finite points return a line \$y=mx+b\$ that evenly splits the points on both sides. Specs Take a list of distinct points \$(x,y)\$ (or a list of x-...
math scat's user avatar
  • 7,607
5 votes
2 answers
862 views

How spherical is my ellipsoid?

Define the (unnormalised) Willmore energy of a surface as the integral of squared mean curvature over it: $$W=\int_SH^2\,dA$$ For surfaces topologically equivalent to a sphere \$W\ge4\pi\$, and \$W=4\...
Parcly Taxel's user avatar
  • 3,637
15 votes
9 answers
1k views

Ptolemy's table of chords

Ptolemy's Almagest contains a table of chords that effectively served as the world's only comprehensive trigonometric table for over a millennium. In modern form it looks like this: \begin{array}{|l|...
Parcly Taxel's user avatar
  • 3,637
8 votes
14 answers
678 views

Time to shortest permutation

Yesterday, as part of a IQ-style test, I got this interesting question: The time on a 24-hour clock is 11:43. What is the least number of minutes I should wait before the same digits are on the ...
UndoneStudios's user avatar
22 votes
27 answers
2k views

Sum of Consecutive Squares

Your task Given a integer input, \$ n \$ (such that \$ n > 1 \$), decide whether it can be written as the sum of (at least 2) consecutive square numbers. Test cases Truthy: ...
The Thonnu's user avatar
  • 11.5k
8 votes
17 answers
2k views

Capture the Flag... with a twist

Inspired by a challenge from the OUCC 2022 Seniors competition. Background Two teams are playing "capture the flag". They take turns invading each other's base and capturing their opposing ...
The Thonnu's user avatar
  • 11.5k
18 votes
4 answers
595 views

Intersection area of two rotated rectangles

Given two rectangles, which are possibly not in the orthogonal direction, find the area of their intersection. Input You may take the rectangles as input in one of the following ways: The ...
alephalpha's user avatar
  • 41.9k
13 votes
10 answers
2k views

Round to nicer numbers

The standard way to round numbers is to choose the nearest whole value, if the initial value is exactly halfway between two values, i.e. there is a tie, then you choose the larger one. However where I ...
Wheat Wizard's user avatar
  • 94.5k
12 votes
10 answers
885 views

Maximum of outer product of integer vectors (in linear time)

Introduction Our goal is to efficiently find the maximum of a large amount of (redundant) data. We define the outer product of vectors \$A\$ and \$B\$ as a matrix containing the products of all ...
Sebastian's user avatar
  • 221
9 votes
3 answers
548 views

RADD decomposition of an integer

Introduction The \$RADD(n)\$ operation is defined as the sum of \$n + [\$ the number whose decimal representation are the decimal digits of \$n\$ in reverse order \$]\$, see A004086. After reversal, ...
Hugo Pfoertner's user avatar
5 votes
7 answers
1k views

Implement the Riemann R function

The Riemann R function is as follows: $$R (x)=\sum _{n=1}^{\infty } \frac{\mu (n) \text{li}\left(x^{1/n}\right)}{n}.$$ This uses the Möbius function as well as the logarithmic integral. From Wikipedia,...
Simd's user avatar
  • 379
11 votes
17 answers
414 views

Normal Subgroups of \$S_4\$

Objective Given a permutation of 4 distinct items, classify the permutation by the normal subgroup(s) it belongs. Input/Output Format You gotta choose the followings as the hyperparameters for your ...
Dannyu NDos's user avatar
  • 4,637
13 votes
8 answers
1k views

Calculate my income tax

Background Here in the UK1, these are the income tax rules: You get a personal allowance (untaxed) of up to £12,570: If you earn less than £100,000, you get the full £12,570 as personal allowance ...
The Thonnu's user avatar
  • 11.5k
31 votes
53 answers
4k views

Sum every second digit in a number

I have a number like this: n = 548915381 The output should be the sum of every second number. In this case 26: 4+9+5+8 = 26 ...
S-Flavius's user avatar
  • 421
20 votes
4 answers
2k views

Write a number as a sum of Fibonacci numbers

In 2009, Hannah Alpert described the "far-difference" representation, a novel way of representing integers as sums and differences of Fibonacci numbers according to the following rules: ...
Peter Kagey's user avatar
  • 8,619
13 votes
25 answers
1k views

Output the length of (the length plus a message) [duplicate]

The task is simple. You're given an arbitrary string message. Return that message prefixed with a number, such that the length of that number plus the message equals the number. In other words, the ...
virchau13's user avatar
  • 489
10 votes
9 answers
432 views

CGAC2022 Day 3: \$n\$-dimensional Chocolate Pyramid

Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details. I've got an infinite supply of \$n\$-dimensional chocolate for some positive integer \$n\$. The shape of the ...
alephalpha's user avatar
  • 41.9k
23 votes
19 answers
2k views

CGAC2022 Day 1: Let's build a chocolate pyramid!

Following last year's event, we're doing Code Golf Advent Calendar 2022! On each day from today (Dec 1) until Christmas (Dec 25), a Christmas-themed challenge will be posted, just like an Advent ...
Bubbler's user avatar
  • 70.2k
5 votes
2 answers
203 views

Transform a lattice polygon to minimum diameter by shearing

Given is a grid polygon by the list of its integer vertex coordinates arranged along the perimeter, in the form \$(x_1,y_1), (x_2,y_2), \cdots , (x_n,y_n)\$ with \$n \ge 3\$. The polygon is completed ...
Hugo Pfoertner's user avatar
16 votes
5 answers
492 views

Perfect Nontransitive Sets

Background For the purposes of this challenge, we'll define a "perfect nontransitive set" to be a set \$A\$ with some irreflexive, antisymmetric relation \$<\$, such that for all \$a \in ...
CursorCoercer's user avatar
23 votes
46 answers
2k views

Maximum average ord

Your task Take a list of strings as the input, and output the maximum average ord. Example Given the list ...
The Thonnu's user avatar
  • 11.5k
20 votes
10 answers
879 views

Counting Stripey Bracelets

A bracelet consists of a number, \$\mathit{N}\$, of beads connected in a loop. Each bead may be any of \$\mathit{C}\$ colours. Bracelets are invariant under rotation (shifting beads around the loop) ...
Jonathan Allan's user avatar
23 votes
29 answers
2k views

Power sequence differences

Your task Given two positive integers \$x\$ and \$d\$ (such that \$d<x\$), output the 5th term of the \$d\$th difference of the sequence \$n^x\$ Example Let's say we are given the inputs \$x=4\$ ...
The Thonnu's user avatar
  • 11.5k

1
2 3 4 5
35