Questions related to elementary arithmetic.

learn more… | top users | synonyms

4
votes
9answers
46 views

Inverse function

Wouldn't it be neat if programming functions could be inverted, just like the mathematical function they implement? Write a function (or program) that takes one input x in any form, that outputs ...
20
votes
33answers
2k views

Electrical outlet

This is a problem from NCPC 2005. Roy has an apartment with only one single electrical outlet, but he has a bunch of power strips. Compute the maximum number of outlets he can have using the power ...
22
votes
16answers
2k views

The RATS sequence

Your task is to generate the nth term of the RATS sequence, where n is the input. The RATS sequence is also known as the Reverse Add Then Sort sequence. This sequence can also be found here: ...
18
votes
23answers
2k views

Distance between two points in n-dimensional space

Here is another simple one: The Challenge Given two points in an n-dimensional space, output the distance between them, also called the Euclidean distance. The coordinates will be rational ...
18
votes
12answers
1k views

What's the point?

Write a program or function that takes in two integers that represent the X and Y coordinates of a point on a Cartesian plane. The input may come in any reasonable format as long as the X value comes ...
13
votes
3answers
737 views

Decipher the mathematical symbols

If you've read the book Contact by Carl Sagan, this challenge may seem familiar to you. Given an input of a set of mathematical equations consisting of a number, an unknown operator, another ...
22
votes
5answers
439 views

Fractal Smoke Sequence

Introduction A229037 has a quite intriguing plot (at least for the first few terms): There is the conjecture, that it might indeed have some kind of fractal property. How is this sequence ...
13
votes
8answers
308 views

Smooth a line graph

Given an input of an integer n and a list of positive integers m1, m2, ..., output a list of integers m1', m2', ... where mx' is defined as the average of mx-n through mx+n. When calculating these ...
14
votes
2answers
117 views

Add and multiply perplexing numbers

The split-complex numbers, also known as "perplex numbers" are similar to the complex numbers. Instead of i^2 = -1, however, we have j^2 = 1; j != +/-1. Each number takes the form of z = x + j*y. ...
23
votes
17answers
1k views

How many peaks in my mountain range?

A list of positive integers can be visualized as a quantized mountain range where each list entry represents the height of one vertical section of the mountains. For example, the list 1, 2, 2, 3, 4, ...
9
votes
2answers
91 views

Smooth vs. Chunky vs. Broken Squiggles

Based on Chunky vs. Smooth Strings. Squiggles /\_/\/\__/\/\/\/\_/\_/\ are fun to make on a keyboard when you are really bored. But not all squiggles are created equal. Some squiggles are smooth, ...
53
votes
26answers
1k views

Expand the number

You may remember in first or second grade using expanded form to learn about place value of numbers. It's easier to explain with an example, so consider the number 123. In expanded form it is ...
17
votes
8answers
651 views

Sort a concatenated sequence

Consider a sequence based on recurrence relations, f(n) = f(n-1)+f(n-2), starting with f(1) = x1, f(2) = x2. For x1 = 2, x2 = 1, the sequence begins like this: 2 1 3 4 7 11 18 29 47 76 123 ...
24
votes
6answers
3k views

1+1 = 10, 1+2 = 3

Write a function or program than can do simple arithmetic (addition, subtraction, multiplication and division) in both base 10 and base 2. The function will take a mathematical expression as input, ...
17
votes
16answers
528 views

Output the Partial Products

In long multiplication, after multiplying the numbers, you are left with the partial products, in this challenge you will output those partial products. Because long multiplication is long, to ...
22
votes
15answers
909 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, ...
18
votes
3answers
427 views

Parenthesize an expression

Recently I've been writing a new language, to avoid needing to handle order of operations, I simply parenthesize each expression properly to avoid this entirely. Because parenthesis are at ...
15
votes
6answers
779 views

D&D point buy cost

When making a Dungeons & Dragons character, an alternative to rolling ability scores is to assign them within a power budget called point buy. Higher ability scores cost more points, especially ...
28
votes
16answers
2k views

How much present did you get for Christmas?

Yes, how much, not how many... As we all know, a large present is far better than a small one. Therefore, the value of the presents should always be measured in total volume, not number of presents, ...
9
votes
2answers
219 views

Solve a mathematical expression from alternating sides

Create a program that solves a mathematical expression using the elements from alternating sides of the expression. The way it’s done is, instead of reading from left to right, you read the first ...
45
votes
31answers
3k views

Alex-style Addition

Inspired by Alex's glorious Learn you an R for great good, we are going to humbly recreate Alex's "one true R program" -- but with a twist. Alex-style Addition works like this -- it has a 90% chance ...
16
votes
9answers
541 views

Seven's Cycle Sum Sequence

Take a look at the sevens multiplication table from 7×0 to 7×9: 0, 7, 14, 21, 28, 35, 42, 49, 56, 63 If we just look at the digits in the one's place we get a permutation of the digits ...
10
votes
9answers
544 views

Arithmetic Progressions

Your task is to analyse the input and output the formula for the n-th term if it is an arithmetic sequence, otherwise it should print "NAAP". Input Input (from STDIN) will consist of few numbers, ...
32
votes
13answers
4k views

The sum is always 15

Write a program or function that takes an array of non-negative integers as input and outputs a set of vectors/arrays with the elements of the input array in order, split so that each vector sums up ...
64
votes
22answers
6k views

Well that's odd… no wait, that's even!

Preamble Integers are always either even or odd. Even integers are divisible by two, odd integers are not. When you add two integers you can infer whether the result will be even or odd based on ...
15
votes
4answers
480 views

How many squares, cubes, fourth powers, etc. do I need to sum to n?

You are given a nonnegative integer n and an integer p >= 2. You need to add some p-th powers (p=2 means squares, p=3 means cubes) together to get n. This is always for any nonnegative n, but you ...
9
votes
6answers
607 views

Calculate the Kronecker symbol

Relevant links here and here, but here is the short version: You have an input of two integers a and b between negative infinity and infinity (though if necessary, I can restrict the range, but the ...
21
votes
20answers
1k views

Is q a quadratic residue of n?

Given two inputs q n determine if q is a quadratic residue of n. That is, is there an x where x**2 == q (mod n) or is q a square mod n? Input Two integers q and n, where q and n are any integers 0 ...
36
votes
19answers
3k views

Triangulating Text

Write a program or function that takes in a string guaranteed to only contain printable ASCII characters except for space, and to be a positive triangular number (1, 3, 6, 10, 15, ...) in length. ...
17
votes
18answers
1k views

Reverse and square

In this challenge you will compute numbers from a curious sequence. Your input is a single decimal nonnegative integer. Reverse the bits in this integer and then square the number to get the required ...
29
votes
23answers
1k views

Very Simple Grid Marks

Write a program or function that takes in three positive integers, W, H, and N. Print or return a W×H grid of .'s where every Nth . in normal English reading order is replaced with an X. For ...
13
votes
6answers
354 views

The unique multiplication spiral

This one is inspired by Calvin's Hobbies recent multiplication table challenge. Write a function or program that takes an integer N as input and prints or returns a N-by-N unique multiplication ...
26
votes
33answers
1k views

Sort the unique numbers in a multiplication table

Pretty simple challenge today: Write a program or function that takes in a positive integer N and prints or returns a sorted list of the unique numbers that appear in the multiplication table whose ...
13
votes
15answers
937 views

Simple Task Solved Thrice

You should write 3 programs and/or functions in one language. All of these programs should solve the same task but they all should give different (but valid) outputs. (I.e. for every pair of programs ...
15
votes
8answers
1k views

Digital Cellular Automata

Write a program or function that takes in an odd positive integer N and a string of decimal digits (0123456789). The string represents a ten-state one-dimensional cellular automaton. Each digit ...
27
votes
3answers
892 views

Integer Complexity

Your task is to write a program that, on input n, outputs the minimal expression of each number 1 through n in order. The shortest program in bytes wins. A minimal expression combines 1's with ...
17
votes
7answers
575 views

Finding sum-free partitions

Executive summary Given input k, find a partition of integers 1 to n into k sum-free subsets for the largest n you can within 10 minutes. Background: Schur numbers A set A is sum-free if its ...
54
votes
28answers
4k views

Stack Exchange Vote Simulator

Write a program or function that takes in a string only containing the characters ^ and v (you can assume there will be no other characters). Read from left to right this string represents the ...
17
votes
11answers
630 views

Calculate the bounded cumulative sum of a vector

The cumulative sum of a vector is calculated by simply taking the sum of all previous elements. For instance: vec = [1 1 1 -1 -1 -1 -1 -1 1 1 1 1 -1] cum_vec = [1 2 3 2 1 0 -1 -2 -1 0 ...
13
votes
1answer
385 views

Dames, do some math!

The order of operations, PEMDAS, is a basic rule in mathematics telling us which order operations should be performed: "Parentheses, Exponents, Multiplication and Division, and Addition and ...
23
votes
8answers
1k views

Print all 3 by 3 sturdy squares

A sturdy square (akin to a magic square) is an arrangement of the integers 1 to N2 on an N by N grid such that every 2 by 2 subgrid has the same sum. For example, for N = 3 one sturdy square is 1 5 ...
22
votes
24answers
1k views

Iterated partial sums

The partial sums of a list of integers [a1, a2, a3, ..., an] are s1 = a1 s2 = a1 + a2 s3 = a1 + a2 + a3 ... sn = a1 + a2 + ... + an We can then take the list of partial sums [s1, s2, s3, ..., sn] ...
25
votes
15answers
1k views

Illustrate the square of a binomial

Given (by any means) two different natural numbers (of any reasonable size), output (by any means) the square of their sum as in the examples below: Given 4 and 3, output: 12 12 12 12 9 9 9 12 12 ...
17
votes
22answers
2k views

Calculate Standard Deviation

Challenge Given a list of numbers, calculate the population standard deviation of the list. Use the following equation to calculate population standard deviation: Input The input will a list of ...
11
votes
7answers
788 views

Averages of Angles

Story, or why we are doing this. None. This exercise is completely pointless ... unless you are Steven Hawking. The Challenge Given a list of angles, find the average of those angles. For example ...
35
votes
7answers
2k views

Cramming The Gramming - Twelve Task Tweet

Your boss just emailed you a list of 12 programming tasks he needs done as soon as possible. The tasks are simple enough but your boss, being a young software tycoon suckled by social networking, ...
31
votes
4answers
2k views

Invert a Minesweeper Board

Minesweeper is a popular computer game that you have probably wasted time playing where you try to reveal the cells that are mines in a rectangular grid based on hints of how many neighboring mines ...
60
votes
115answers
10k views

1, 2, Fizz, 4, Buzz

Introduction In our recent effort to collect catalogues of shortest solutions for standard programming exercises, here is PPCG's first ever vanilla FizzBuzz challenge. If you wish to see other ...
2
votes
5answers
591 views

Isn't the point of math to just use numbers?

Write a program that converts a mathematical expression to words, evaluates the expression, and prints how the equation would be spoken aloud. Input The input must only contain numbers and the ...
38
votes
24answers
2k views

Compute the Digit Difference Sum of a Number

Consider taking some non-negative integer such as 8675309 and computing the absolute values of the differences between all the pairs of neighboring digits. For 8675309 we get |8-6| = 2, |6-7| = 1, ...