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
41 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 ...
3 votes
1 answer
85 views

Generate a Kirkman triple system

Given a universe of \$v\$ elements, a Kirkman triple system is a set of \$(v-1)/2\$ classes each having \$v/3\$ blocks each having three elements, so that every pair of elements appears in exactly ...
15 votes
30 answers
3k views

The inverse Collatz Conjecture

I think the Collatz Conjecture is already well-known. But what if we invert the rules? Start with an integer n >= 1. Repeat the following steps: If n is even, multiply it by 3 and add 1. If n is ...
16 votes
20 answers
2k views

Are they collinear?

Task Write a program/function that when given three 2d points in cartesian coordinates as input outputs a truthy value if they are collinear otherwise a falsey value Three points are said to be ...
19 votes
22 answers
2k views

Anti-divisors of a number

Given a positive integer n, output all of its anti-divisors in any order. From OEIS A006272: Anti-divisors are the numbers that do not divide a number by the ...
58 votes
47 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 ...
13 votes
19 answers
828 views

Cartesian - polar conversion couple

We don't have a challenge for conversion between Cartesian and polar coordinates, so ... The challenge Write two programs (or functions) in the same language: one that converts from polar to ...
43 votes
35 answers
2k 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 ...
10 votes
28 answers
1k views

Are the numbers amicable?

Two numbers are considered amicable if the proper divisor sum of the first is the same as the second number, the second number's proper divisor sum is equal to the first number, and the first and ...
25 votes
28 answers
2k views

Find a Fixed Point

Given an integer \$x_1\$ and some black box function \$f: ℤ → ℤ\$ find a fixed point of \$f\$ in the sequence defined by \$x_{k+1} := f(x_k)\$. Details A value \$x\$ is said to be a fixed point of \$...
21 votes
27 answers
1k views

Divisor reduction

A divisor of a number n is any number that evenly divides n, including 1 and n itself. The number of divisors d(n) is how many divisors a number has. Here's d(n) for the first couple n: ...
4 votes
5 answers
2k views

Convert from postfix notation to infix notation

This question may not be that creative, but I believe that it is different enough to pose a new challenge. The Challenge Your challenge is to write the shortest program possible that can take any ...
15 votes
15 answers
1k views

Output the nth Even Perfect Number

Challenge Given an integer, n, as input where 0 <= n <= 2^10, output the nth even perfect number. Perfect Numbers A perfect number is a number, x where the ...
12 votes
6 answers
571 views

Implement Multiplicative Fuzzy Logic

Inspired by this excellent challenge (from which the bulk of this text is blatantly duct-taped) – and my highschool philosophy project... I define the following operators: Fuzzy Conjunction a ×F b is ...
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 ...
21 votes
24 answers
2k views

Delannoy numbers

Consider a grid from \$(0,0)\$ in the bottom-left corner to \$(m,n)\$ in the top-right corner. You begin at \$(0,0)\$, and can only move in one of these three ways: Directly north \$(+0, +1)\$, ...
28 votes
42 answers
5k views

The plus-minus sequence

The plus-minus sequence The plus-minus sequence is one that starts with two seeds, a(0) and b(0). Each iteration of this ...
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 ...
19 votes
20 answers
2k views

How many partitions do I have?

The partition number of a positive integer is defined as the number of ways it can be expressed as a sum of positive integers. In other words, the number of integer partitions it has. For example, the ...
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 ...
44 votes
90 answers
5k views

Construct the Identity Matrix

The challenge is very simple. Given an integer input n, output the n x n identity matrix. The identity matrix is one that has <...
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: ...
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 ...
38 votes
42 answers
8k views

Is this a triangle?

Task Write a function/program that, given three positive integers a, b and c, prints a ...
19 votes
14 answers
878 views

Goldbach partitions

The Goldbach conjecture states that every even number greater than two can be expressed as the sum of two primes. For example, 4 = 2 + 2 6 = 3 + 3 8 = 5 + 3 ...
27 votes
44 answers
7k views

Additive Persistence

The shortest code to pass all possibilities wins. In mathematics, the persistence of a number measures how many times a certain operation must be applied to its digits until some certain fixed ...
5 votes
83 answers
4k views

Summation from a to b

The program has an input of a and b, and outputs the sum of numbers from a to b, inclusive. Score is in bytes. As always, standard loopholes are disallowed. If your input is in the format [a,b], +3 ...
25 votes
34 answers
4k views

Strange Addition

Challenge Calculate the strange sum of two natural numbers (also known as lunar addition): Given \$A=...a_2 a_1 a_0\$ and \$B=... b_2 b_1 b_0\$ two natural numbers written in the decimal base, the ...
44 votes
46 answers
8k views

Shift right by half a bit

The challenge is to implement a program or function (subsequently referred to as "program") that takes a nonnegative integer \$n\$ as input and returns \$n\over\sqrt{2}\$ (the input divided by the ...
18 votes
83 answers
5k views

Average Two Letters

Introduction Every letter in the English alphabet can be represented as an ASCII code. For example, a is 97, and ...
20 votes
26 answers
2k views

Triangle area from side lengths

Output the area \$A\$ of a triangle given its side lengths \$a, b, c\$ as inputs. This can be computed using Heron's formula: $$ A=\sqrt{s(s-a)(s-b)(s-c)}\textrm{, where } s=\frac{a+b+c}{2}.$$ This ...
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 ...
34 votes
25 answers
3k views

Sums of Consecutive Integers

Before anyone says anything, similar and similar. But this is not a dupe. Some positive integers can be written as the sum of at least two consecutive positive integers. For example, ...
10 votes
2 answers
348 views

Partition and Restructure

Given two contiguous shapes of the same area, determine the optimal way to divide the first shape into a minimum number of contiguous segments such that they can be rearranged to form the second shape....
45 votes
114 answers
6k 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/...
48 votes
114 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 ...
44 votes
42 answers
12k views

Fibonacci reversed!

Introduction We all know and love our Fibonacci sequence and have seen a myriad of challenge on it here already. However, we're still lacking a very simple case which this answer is going to provide: ...
28 votes
48 answers
5k views

Is that number a Two Bit Number™?

Let's start by defining a Two Bit Number™: It is a positive integer When expressed as a binary string it has exactly 2 true bits OR When expressed as a decimal number, it has exactly 2 of the numeral ...
6 votes
18 answers
855 views

Written Word Equation

Word equations, but not as you know it! Given a sentence which will include two numbers, numerically, and a spelt operator, in the order seen in the examples, your goal is to give the numerical answer ...
16 votes
30 answers
5k 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, ...
41 votes
53 answers
7k views

Don't give me five!

Question : You will be given the starting and ending integers of a sequence and should return the number of integers within it which do not contain the digit 5. The ...
12 votes
6 answers
735 views

Exponential transform of an integer sequence

The exponential generating function (e.g.f.) of a sequence \$a_n\$ is defined as the formal power series \$f(x) = \sum_{n=0}^{\infty} \frac{a_n}{n!} x^n\$. When \$a_0 = 0\$, we can apply the ...
23 votes
50 answers
4k views

Get the best of two Arrays

You will be given two arrays of floating-point numbers. Your task is to pair the corresponding elements of the two arrays, and get the maximum of each pair. However, if the two corresponding elements ...
40 votes
67 answers
5k views

Infinitely many ℕ

Background: A sequence of infinite naturals is a sequence that contains every natural number infinitely many times. To clarify, every number must be printed multiple times! The Challenge: Output a ...
45 votes
66 answers
4k views

Can this number be written in (3^x) - 1 format?

Challenge: Create a program that accepts a positive integer and checks if it can be written in the form of (3^x)-1, where X is another positive integer. If it can, output X If it can't, output -1 ...
-3 votes
37 answers
1k views

Is it a completely even number?

A completely even number is a positive even integer whose divisors (not including 1) are all even. Some completely even numbers are: 2 (divisors: 2, 1) 4 (divisors: 4, 2, 1) 16 (divisors: 16, 8, 4, 2,...
33 votes
52 answers
2k views

The Squaring Sequence

Each term in the squaring sequence, \$x_n\$, is created by taking \$x_{n-1}\$, squaring it, and removing all but the first four digits. The sequence always begins with \$x_1 = 1111\$. Squaring this ...
-6 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) ...
23 votes
77 answers
2k views

Raise integer x to power x, without exponentiation built-ins

Task - The title pretty much sums it up: raise an integer x to power x, where 0<x. Restrictions: Use of exponentiation, ...
32 votes
135 answers
9k views

Multiply two numbers

Input: Two decimal integers. These can be given to the code in standard input, as arguments to the program or function, or as a list. Output: Their product, as a decimal integer. For example, the ...

1
2 3 4 5
34