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
391 votes
156 answers
288k 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. ...
qwr's user avatar
  • 11.5k
326 votes
55 answers
111k 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 ...
A Frayed Knot's user avatar
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 ! ...
Calvin's Hobbies's user avatar
175 votes
101 answers
40k views

Programming Languages Through The Years

In this challenge, users will take turns completeing three fairly simple coding tasks in programming languages that are allowed to be progressively older. The first answer must use a programming ...
Calvin's Hobbies's user avatar
168 votes
6 answers
18k 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 ...
Calvin's Hobbies's user avatar
134 votes
11 answers
14k views

(-a) × (-a) = a × a

We all know that \$(-a) \times (-a) = a \times a\$ (hopefully), but can you prove it? Your task is to prove this fact using the ring axioms. What are the ring axioms? The ring axioms are a list of ...
Wheat Wizard's user avatar
  • 96.5k
109 votes
406 answers
20k 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 ...
caird coinheringaahin g's user avatar
107 votes
12 answers
10k views

Proving that a Russian cryptographic standard is too structured

The aim of this challenge is to find an impossibly short implementation of the following function p, in the langage of your choosing. Here is C code implementing it ...
picarresursix's user avatar
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 ...
Bolton Bailey's user avatar
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 ...
Mark Jeronimus's user avatar
91 votes
14 answers
6k views

The Colors in Our Stars

Write a program that takes in a string or text file whose first line has the form width height and each subsequent line has the form ...
Calvin's Hobbies's user avatar
90 votes
98 answers
20k views

When does (x == x+2)? [closed]

The challenge: Define x in such a way that the expression (x == x+2) would evaluate to true. I tagged the question with C, but ...
87 votes
135 answers
13k 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 ...
Doorknob's user avatar
  • 71.4k
85 votes
222 answers
17k 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 ...
GarethPW's user avatar
  • 1,299
85 votes
6 answers
41k views

Hard code golf: Regex for divisibility by 7

Matthias Goergens has a 25,604-character (down from the original 63,993-character) regex to match numbers divisible by 7, but that includes a lot of fluff: redundant parentheses, distribution (...
Charles's user avatar
  • 2,741
83 votes
192 answers
10k 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
74 votes
16 answers
15k views

Calculate the number of primes up to n

π(n) is the number of primes less than or equal to n. Input: a natural number, n. Output: π(n). Scoring: This is a fastest-code challenge. Score will be the sum of times for the score cases. I ...
Liam's user avatar
  • 3,205
72 votes
26 answers
9k views

Yo boy, must it sum

Every positive integer can be expressed as the sum of at most three palindromic positive integers in any base b≥5.   Cilleruelo et al., 2017 A positive integer is palindromic in a given base if ...
Luis Mendo's user avatar
  • 104k
65 votes
10 answers
5k views

How lit is this mountain? 🔥

A mountain is defined to be a set of line segments whose first point has coordinates (0,a) where a > 0, and whose last point ...
rigged's user avatar
  • 1,543
63 votes
44 answers
14k 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 ...
isaacg's user avatar
  • 42k
63 votes
38 answers
9k views

Factorial in haiku!

Task Create a program that calculates the factorial of a number using no built-in factorial functions. Easy? The catch is that you must write your entire program (including testing it) in haiku form. ...
TheDoctor's user avatar
  • 7,989
63 votes
44 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 ...
DJMcMayhem's user avatar
  • 58.9k
63 votes
15 answers
18k views

9 Hole Challenge [closed]

The 9 Hole Challenge 9 code golfing challenges of varying difficulty. Penalties for using the same language more than once. The question will be updated with pars, hole champions and trophy winners....
Dan Prince's user avatar
  • 1,467
61 votes
40 answers
9k views

Calculate a square and a square root!

You must make a polyglot that outputs the square of the input in one language and the square root of the input in another. The shortest answer in bytes wins! You must have a precision of at least 3 ...
user avatar
60 votes
49 answers
5k views

How many steps does it take from n to 1 by subtracting the greatest divisor?

Inspired by this question over at Mathematics. The Problem Let n be a natural number ≥ 2. Take the biggest divisor of ...
insertusernamehere's user avatar
58 votes
10 answers
10k views

Little Chandler is sad. Draw him a cloud to cheer him up

Little Chandler is sad. Draw him a cloud to cheer him up. Note: Drawing a cloud won't actually cheer him up. A circle can be defined as a 3-tuple (x,y,r) where <...
absinthe's user avatar
  • 8,619
57 votes
77 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^...
Iszi's user avatar
  • 2,538
57 votes
57 answers
6k views

Count without 3

Background When I was in elementary school, we used to play a game in math class that goes as follows. All kids sit in a big circle and take turns counting, starting from 1. However, the following ...
Dennis's user avatar
  • 210k
57 votes
35 answers
7k views

Superstitious hotel elevator

Description Here's a very superstitious hotel elevator in Shanghai:                 It avoids the number 13, because ...
Lynn's user avatar
  • 68.1k
56 votes
36 answers
8k views

Conway's Monster

This challenge is in honor of mathematician John Conway who passed away April 11, 2020 due to COVID-19. He was famous for coming up with the Game of Life cellular automaton and many other mathematical ...
Calvin's Hobbies's user avatar
55 votes
50 answers
9k views

Coprimes up to N

Given a number n >= 2, output all the positive integers less than n where gcd(n, k) == 1 (...
Riker's user avatar
  • 7,800
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 ...
PyRulez's user avatar
  • 7,199
55 votes
22 answers
3k views

Dennis numbers 2.0

PPCG user and elected mod, @Dennis just became the second ever user to earn over 100k rep! This is a totally original idea, that I did not get from anybody else, but let's make a challenge based off ...
DJMcMayhem's user avatar
  • 58.9k
54 votes
146 answers
8k views

The vanilla factorial challenge

Task Given a non-negative integer \$n\$, evaluate the factorial \$n!\$. The factorial is defined as follows: $$ n!=\begin{cases}1 & n=0\\n\times(n-1)!&n>0\end{cases} $$ Rules All default I/...
Bubbler's user avatar
  • 73.8k
53 votes
34 answers
7k views

Golf a transcendental number

Definitions An algebraic number is a number that is a zero of a non-zero polynomial with integer coefficients. For example, the square root of 2 is algebraic, ...
Leaky Nun's user avatar
  • 49.6k
53 votes
66 answers
7k views

Multiplicative persistence

Multiplicative Persistence Multiply all the digits in a number Repeat until you have a single digit left As explained by Numberphile: Numberphile "What's special about 277777788888899?" ...
SQB's user avatar
  • 781
53 votes
18 answers
8k views

Program that adds all natural numbers and yields -1/12 [closed]

As you might know there is a mathematical fun-fact that if you add all natural numbers you end up with... -1/12 (see Wikipedia here). Of course this is very strange result and can not be obtained by ...
Paweł Tokarz's user avatar
53 votes
43 answers
3k views

Illustrate the Least Common Multiple

Given two positive integers, \$A\$ and \$B\$, illustrate their least common multiple by outputting two lines of dashes (-) with length \$\text{lcm}\$\$(A, B)\$ ...
Calvin's Hobbies's user avatar
53 votes
9 answers
4k views

Help me with differential calculus!

I love programming and know every language, but I suck at math. Unfortunately, my school requires that computers students must take a year of calculus. There's a test next week, and I don't know any ...
Ypnypn's user avatar
  • 10.8k
52 votes
81 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 ...
Shaggy's user avatar
  • 41.6k
52 votes
46 answers
5k views

Detect heat waves

Background The Royal Netherlands Meteorological Institute defines a heat wave* as a series of at least 5 consecutive days of ≥25°C weather (“summery weather”), such that at least 3 of those days are ≥...
Lynn's user avatar
  • 68.1k
52 votes
43 answers
15k views

The Snail in the Well

Background There's a common riddle that goes something like this: A snail is at the bottom of a 30 foot well. Every day the snail is able to climb up 3 feet. At night when they sleep, they slide ...
musicman523's user avatar
  • 4,772
52 votes
3 answers
6k views

Quiche Lorraine [closed]

Since it was Pi day recently, I have noticed a number of challenges that ask you to calculate pi. Of course, a quiche lorraine is not quite a pie (you can claim a Bonus Score¹ of +1 if you guessed ...
Sanchises's user avatar
  • 9,446
51 votes
123 answers
5k views

Product over a range

Your task is simple: given two integers \$a\$ and \$b\$, output \$\Pi[a,b]\$; that is, the product of the range between \$a\$ and \$b\$. You may take \$a\$ and \$b\$ in any reasonable format, whether ...
Conor O'Brien's user avatar
51 votes
48 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 ...
xnor's user avatar
  • 144k
51 votes
11 answers
3k views

Bringing a pair of integers to equality

This was inspired by a math problem I saw somewhere on the internet but do not remember where (UPDATE: The original problem was found on the math riddles subreddit with a proof provided that it is ...
JMigst's user avatar
  • 879
50 votes
66 answers
11k views

Divide a number by 3 without using *, /, +, -, % operators [closed]

Quoting this question on SO (Spoiler alert!): This question has been asked in an Oracle interview. How would you divide a number by 3 without using *, /, +, -, %, operators? The number ...
Gaffi's user avatar
  • 3,597
50 votes
81 answers
10k views

Is my triangle right?

Given a, b, c the length of the three sides of a triangle, say if the triangle is right-angled (i.e. has one angle equal to 90 degrees) or not. Input Three positive ...
mdahmoune's user avatar
  • 2,862
50 votes
41 answers
3k views

Moving modest minimum

Inspired by a question over at Stack Overflow. The title here is entirely my fault. The challenge Given a list of positive integers containing at least two entries, replace each number by the ...
Luis Mendo's user avatar
  • 104k
50 votes
5 answers
2k views

Multiply with restricted operations

There's a 500 rep unofficial bounty for beating the current best answer. Goal Your goal is to multiply two numbers using only a very limited set of arithmetic operations and variable assignment. ...
xnor's user avatar
  • 144k

1
2 3 4 5
36