Questions tagged [arithmetic]

Challenges related to elementary arithmetic.

Filter by
Sorted by
Tagged with
24 votes
22 answers
3k views

American odds to probabilities

American odds (aka moneyline odds) are numbers like \$+150\$ or \$-400\$ used to express how much a winning bet would pay out. Convert odds to a fair win probability like this: Positive odds \$+n\$ ...
xnor's user avatar
  • 144k
9 votes
11 answers
719 views

Otteretto Classic game scoring method

Brief description of the game In the game Otteretto Classic (which you can test directly in your browser; try it!) the player has to form palindromic sequences using adjacent cells on a square grid. ...
Luis Mendo's user avatar
  • 104k
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
  • 769
22 votes
25 answers
4k views

Given 4 fence lengths, what's the largest rectangular yard you can make?

Here's a very simple little problem that I don't believe has been asked before. Challenge Write a program or a function that takes in four positive integers that represents the lengths of movable but ...
blaketyro's user avatar
  • 769
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
  • 95.5k
12 votes
71 answers
3k views

Summing the Digits of a Number

Challenge Given a non-negative integer, find the sum of its digits. Rules Your program must take a non-negative integer as input. Your program should output the sum of the digits of the input ...
Aitzaz Imtiaz's user avatar
10 votes
9 answers
865 views

Parse basic arithmetic into an AST

Introduction An abstract syntax tree (AST) is a tree of tokens that shows their connection to each other and syntactical meaning. They are commonly used in compilers and static analysis tools, as ...
Michael M.'s user avatar
16 votes
15 answers
1k views

Division between two strings

Inverse function of this challenge To (properly) multiply an string by an integer, you split the string into characters, repeat each character a number of times equal to the integer, and then stick ...
l4m2's user avatar
  • 19.1k
5 votes
21 answers
503 views

Iterate through addition of number sequence until a single digit [closed]

Challenge: With any given number that has multiple digits, every digit in the number needs to repeatedly be summed together until the sum is a single digit number. To make things interesting, if there'...
Larsenv's user avatar
  • 87
19 votes
25 answers
1k views

Carry-less sum given a base b

Given a list of positive integers \$\mathcal I=I_1,I_2,I_3,...,I_n\$ and a base \$b>1\$ return their "carry-less sum", i.e. represent \$\mathcal I\$ in base \$b\$ and sum digit-by-digit ...
loopy walt's user avatar
  • 15.6k
15 votes
20 answers
1k 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 ...
Luis Mendo's user avatar
  • 104k
19 votes
9 answers
1k views

Cryptic Multiplications

Given two non-negative integers e.g. 27, 96 their multiplication expression would be 27 x 96 = 2592. If now each digits is ...
Domenico's user avatar
  • 2,253
2 votes
0 answers
346 views

Decompose number N into the sum of three triangular numbers [closed]

It is known that any natural number can be decomposed into the sum of three triangular numbers (assuming 0 is triangular), according to Fermat's Polygonal Number Theorem. Your task is to come up with ...
Study's user avatar
  • 45
15 votes
16 answers
1k views

Implement Binary Exponentiation

Background In programming, there is a recursive algorithm called binary exponentiation, which allows for large integer powers to be calculated in a faster way. Given a non-zero base \$x\$ and a non-...
Aiden Chow's user avatar
  • 12.3k
12 votes
17 answers
1k views

Extract the contained powers of two!

Take as input an integer in any reasonable format. Then, output all the proper sub-strings of the base-10 digits of that integer that are powers of two, in any reasonable format. Test cases ...
Qaziquza's user avatar
  • 1,573
5 votes
19 answers
2k views

Palindromic Powers

Powers We define an important power as a number that can be represented as \$ x^y \$ where \$ x ≥ 2 \$ and \$ y ≥ 2 \$. Palindrome We define an ...
Number Basher's user avatar
17 votes
12 answers
2k views

Random Point from a 2D Donut Distribution

A donut distribution (for lack of a better term) is a random distribution of points in a 2-dimensional plane, forming a donut-like shape. The distribution is defined by two parameters: the radius <...
Jitse's user avatar
  • 6,846
9 votes
2 answers
319 views

No parentheses shall be omitted

This expression actually has an omitted pair of parentheses. 1 + 2 * 3 To make things clear, it should be, 1 + (2 * 3) Even ...
xiver77's user avatar
  • 2,325
15 votes
7 answers
3k views

Make S + S + ... + S as Large as Possible!

Let \$S \subset \mathbb N_{\geq0}\$ be a subset of the nonnegative integers, and let $$ S^{(k)} = \underbrace{S + S + \dots + S}_{k\ \textrm{times}} = \{ a_1 + a_2 + \dots + a_k : a_i \in S\}. $$ For ...
Peter Kagey's user avatar
  • 8,649
10 votes
5 answers
603 views

Khinchin's constant bad estimate [closed]

Inspired by How to write down numbers having an infinity of decimals? Link 🇫🇷 Background From Wikipedia: for almost all real numbers \$x\$, coefficients \$a_i\$ ...
Julian's user avatar
  • 2,578
26 votes
27 answers
2k views

Hunt for discount

Story My local pizza delivery introduced new discount. You get 50% discount from every second item on your order. But being greedy capitalists, they forgot to mention that they will rearrange items ...
talex's user avatar
  • 711
28 votes
12 answers
2k views

Rows of the Collatz tree

Consider a binary tree built the following way: The root node is \$1\$ For a given node \$n\$: If \$n\$ is odd, its only child is \$2n\$ If \$n\$ is even, one of its children is \$2n\$. If \$\frac {...
caird coinheringaahin g's user avatar
18 votes
4 answers
671 views

Calculate the integer square root of a matrix

Let \$A\$ be a square matrix that is at least \$2 \times 2\$ where each element is an integer. \$A^2 = A \times A\$ will then have the same dimensions as \$A\$, and will have integer elements. For ...
caird coinheringaahin g's user avatar
15 votes
41 answers
1k views

Index and return 3 characters

The challenge is simple: Read 3 letters from a system input¹ and convert them to numbers (A-1, B-2, C-3...Z-26)². Print each of those numbers in the order the ...
REMCodes's user avatar
  • 301
6 votes
6 answers
393 views

Blinking Lighthouses – Founder's Day Upgrade

To celebrate the island's next Founder's Day, colored filters (fuchsia, chartreuse, and aquamarine*) have been installed in every lighthouse. All lighthouses begin operation with their fuchsia filter,...
DjinTonic's user avatar
  • 636
25 votes
11 answers
2k views

Blinking Lighthouses

Lighthouses on an island have blink codes that let ships know which lighthouse they are seeing. Input should be strings of S and L. Here is a (Python) example of codes for 4 lighthouses as a list of ...
DjinTonic's user avatar
  • 636
39 votes
15 answers
2k views

Maximum number of squares touched by a line segment

Consider a square grid on the plane, with unit spacing. A line segment of integer length \$L\$ is dropped at an arbitrary position with arbitrary orientation. The segment is said to "touch" ...
Luis Mendo's user avatar
  • 104k
19 votes
10 answers
1k views

Ploughed fields via moduli

Let \$R, C\$ be positive integers and let \$0 < s \leq 1\$. Consider the \$R \times C\$ matrix \$\mathbf M\$ defined as \begin{equation} M(i,j) = \frac{\mathrm{mod}\,(j, i^s)}{R^s}, \quad i = 1, \...
Luis Mendo's user avatar
  • 104k
8 votes
3 answers
820 views

Crack the Lost Numbers

The Lost Numbers are 4, 8, 15, 16, 23, 42. Your goal is to create an expression to output them all. Your allowed characters are 0123456789, ^+-*/, (), % for modulo and n as the single variable allowed....
AndrewTheCodegolfer's user avatar
7 votes
3 answers
180 views

Distribute the additions!

Challenge: Given an expressions made of additions and multiplications, output an expression that is a sum of products. The output must be equivalent to the input modulo the law of distribution. For ...
Trebor's user avatar
  • 749
5 votes
10 answers
1k views

Odds that a string of N digits contains two or more of the same

I have to fill in 2fa codes all day. They're 6-digit strings. One day I noticed that not once did any of these codes contain 6 unique digits, like 198532 There was always at least one double, like ...
ToonAlfrink's user avatar
  • 1,276
27 votes
13 answers
3k views

Collatz's ice cream cone factory

The Collatz sequence Given a positive integer \$a_1\$, the Collatz sequence with starting value \$a_1\$ is defined as \begin{equation} a_{n+1} = \begin{cases} a_n/2 & \mathrm{if}\ a_n\ \mathrm{is}\...
Luis Mendo's user avatar
  • 104k
21 votes
14 answers
2k views

Make a "proof" generator

Introduction How to prove that 1 = 2: 1 = 2 2 = 4 (*2) -1 = 1 (-3) 1 = 1 (^2) You can just multiply both sides by 0, but that's cheating. This is just bending the ...
emanresu A's user avatar
  • 34.4k
20 votes
6 answers
1k views

Inserting digits to satisfy equation

You are given 3 non negative numbers: \$x\$, \$y\$ and \$z\$, and must minimize the number of digits (non negative) inserted at any place in the numbers \$x\$, \$y\$, or \$z\$ to make $$x + y = z$$ (a ...
cheems's user avatar
  • 619
11 votes
21 answers
3k views

Exciting Mario Kart Grand Prix - Minimize the point difference!

Introduction When playing Mario Kart the other day, an interesting question popped up when a Grand Prix with my 2 roommates, 9 AI drivers and myself seemed to be fairly close and therefore exciting ...
Der_Reparator's user avatar
14 votes
16 answers
591 views

Sum over an Interval [duplicate]

Inspired by this challenge, and similar to this and this. After seeing these, I figured I would continue the trend as there was no inclusive/exclusive sum. The goal here is to sum an interval of ...
Underslash's user avatar
  • 1,709
1 vote
11 answers
246 views

Implement ΔList [duplicate]

Given a list of integers, such as {1, 4, 2, 8, 10}, TI-Basic's ΔList will determine the difference between every overlapping ...
Rydwolf Programs's user avatar
16 votes
16 answers
3k views

Number of coins needed to make change

Relatable scenario: I'm going to the store to buy a single item, but only have a $100k bill. As a result, I need exactly $99,979 in change, and in the fewest coins/bills possible because I'm quite ...
Rydwolf Programs's user avatar
16 votes
19 answers
1k views

Multiplicative Persistence #2

We had a challenge on Multiplicative Persistence here. As a recap, to get a multiplicative persistence of a number, do these steps: Multiply all the digits of a number (in base \$10\$) Repeat Step 1 ...
math scat's user avatar
  • 7,899
13 votes
14 answers
2k views

Verify the inequality!

Left in sandbox for at least 3 days. I want to verify if this inequality is true: for \$n\geq4\$, if \$a_1,a_2,a_3,\dots,a_n\in R_+\cup\{0\}\$ and \$\sum_{i=1}^na_i=1\$, then \$a_1a_2+a_2a_3+a_3a_4+\...
atzlt's user avatar
  • 487
13 votes
8 answers
709 views

Next to the middle

Given an array of integers, find "the next to the middle". The next to the middle is the smallest integer greater than the smallest among mean, median and mode of the given numbers, that is ...
anotherOne's user avatar
  • 1,081
10 votes
0 answers
165 views

8 out of 10 programmers like countdown [duplicate]

Edit (Jan 24/2021): The original specification for the challenge had a few problems. Underneath the original text, I have added alternate conditions that were suggested by @caird-coinheringaahing (...
Mahkoe's user avatar
  • 281
-1 votes
2 answers
208 views

increment a binary number without arithmetic manipulations [closed]

Input: binary string Output: binary string that represents the binary input + 1 Example: Given 10111 as an input, output 11000 ...
Elimination's user avatar
39 votes
38 answers
3k views

Ken Iverson’s Favourite APL Expression?

Ken Iverson, 1920–2020 Let's implement his favourite expression: Given a row of Pascal's triangle, compute the next row. This can for example be computed by taking the input padded with a zero on the ...
Adám's user avatar
  • 29.8k
33 votes
52 answers
4k views

Multiply or Divide by n

Here's a simple challenge, so hopefully lots of languages will be able to participate. Given a positive integer \$n\$, output \$A076039(n)\$ from the OEIS. That is, start with \$a(1)=1\$. Then for \$n&...
Giuseppe's user avatar
  • 27.9k
-1 votes
1 answer
210 views

Square root of a base 10 positive integer, digit by digit [closed]

It is possible (Wikipedia reference) to compute the square root of any number in a digit by digit fashion using a simple (I had learned it in primary school) algorithm. Challenge: read a positive ...
gboffi's user avatar
  • 115
14 votes
9 answers
2k views

We're much closer than you think

Martin Ender's 2D programming language Alice has two different modes depending on what orientation the IP has: orthogonal (Cardinal mode) or diagonal (Ordinal mode). Commands in Alice change their ...
caird coinheringaahin g's user avatar
34 votes
7 answers
4k views

Shortest "arithmetic" formula to output 1000 primes

Write a formula using only the digits 0-9, +, *, -, <...
prime_directive's user avatar
15 votes
16 answers
1k views

Concatenations of powers and their squares

At time of writing, my reputation is \$16,256\$. As I noted in chat, Oh cool my rep is the concatenation of two powers of 2: 16,256 Or even the concatenation of a power of 2 and its square, which is ...
caird coinheringaahin g's user avatar
54 votes
144 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.1k

1
2 3 4 5
11