The challenge involves mathematics. Also consider using more specific tags: [tag:number] [tag:number-theory] [tag:arithmetic] [tag:combinatorics].

learn more… | top users | synonyms

22
votes
31answers
4k views

Rand5() to Rand7()

You are provided with a function Rand5(). This function returns perfectly random (equal distribution) integers between 1 and 5. Provide the function Rand7(), which uses Rand5() to produce perfectly ...
20
votes
25answers
3k views

Draw A Sierpinski Triangle

The Sierpinsky Triangle is a fractal created by taking a triangle, decreasing the height and width by 1/2, creating 3 copies of the resulting triangle, and place them such each triangle touches the ...
16
votes
29answers
6k views

Determine if 4 points form a square

Write a function that takes 4 points on the plane as input and returns true iff the 4 points form a square. The points will have integral coordinates with absolute values < 1000. You may use any ...
15
votes
23answers
1k views

Reverse Polish notation

You must evaluate a string written in Reverse Polish notation and output the result. The program must accept an input and return the output. For programming languages that do not have functions to ...
14
votes
3answers
2k views

Hard code golf: Regex for divisibility by 7

Matthias Goergens has a 63,993-character regex to match numbers divisible by 7, but that includes a lot of fluff: redundant parentheses, distribution (xx|xy|yx|yy rather than [xy]{2}) and other ...
13
votes
36answers
3k views

Adding without using a + or - sign

There have been many "Do __ without __" challenges before, but I hope that this is one of the most challenging. The Challenge You are to write a program that takes two natural numbers (whole numbers ...
13
votes
29answers
2k views

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

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 ...
13
votes
19answers
884 views

Give the smallest number that has N divisors

Your function takes a natural number and returns the smallest natural number that has exactly that amount of divisors, including itself. Exemples: f(1) = 1 [1] f(2) = 2 [1, 2] f(3) = 4 [1, 2, 4] ...
13
votes
10answers
850 views

Good rational approximations of pi

Write a program that prints out all the good rational approximations of pi with denominator < 1000000, in increasing denominator order. a/b is a "good rational approximation" of pi if it is closer ...
13
votes
14answers
1k views

My Word can beat up your Word

PROBLEM Given two words, find the winner in a digital root battle. Define the digital root of a word this way: Each letter of the alphabet is assigned a number: A = 1, B = 2, C = 3, ..., Z = 26 ...
13
votes
4answers
470 views

Determine if a polygon is convex

Write a program to determine if the input polygon is convex. The polygon is specified with one line containing N, the number of vertices, then N lines containing the x and y coordinates of each ...
12
votes
4answers
576 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 ...
11
votes
28answers
2k views

Generate Pascal's triangle

Pascal's triangle is generated by starting with a 1 on the first row. On subsequent rows, the number is determined by the sum of the two numbers directly above it to the left and right. To ...
11
votes
23answers
1k views

Implement hyperexponentiation/tetration without the use of '^'

The Challenge Implement tetration (aka Power Tower or Hyperexponentiation) with the least amount of characters. The Conditions Don't use the 'power' operator or its equivalents (such as pow(x,y), ...
11
votes
5answers
469 views

1P5: Earthquake!

USGS's seismometers have just detected a major earthquake! Emergency response teams need a quick estimate of the number of people affected. Write a program to compute this estimate. Your program ...
11
votes
2answers
493 views

Lego gear ratios

I'm building a giant lego robot and I need to generate some particular gear ratios using a set of gears. I have lots of gears with the common lego gear sizes: 8, 16, 24, or 40 teeth. Write a program ...
10
votes
7answers
780 views

Sum of (at most) 5 primes

Terence Tao recently proved a weak form of Goldbach's conjecture! Let's exploit it! Given an odd integer n > 1, write n as a sum of up to 5 primes. Take the input however you like, and give ...
10
votes
5answers
544 views

Simulate any 1D cellular automaton

The Challenge You are to write a complete program that takes seven numbers from STDIN, and prints the two dimensional history of the cellular automaton (CA) to STDOUT. This is code golf. Input ...
10
votes
5answers
639 views

Advanced Calculator

You must write a program that evaluates a string that would be entered into an advanced calculator. The program must accept input using stdin and output the correct answer. For languages that do no ...
9
votes
13answers
631 views

Non trivial powers

Write the shortest program to print the non-trivial powers ≤ 2^12 in order non-trivial power means the base and exponent are > 1 Leading whitespace is optional When there are multiple solutions, ...
9
votes
5answers
476 views

Reverse Engineer Polling Statistics

Introduction Given a set of percentages of choices in a poll, calculate the minimum number of voters there must be in the poll to generate those statistics. Example: What is your favorite pet? ...
9
votes
6answers
2k views

Count number of hefty decimals between 2 numbers

Let's say we have a non-negative integer that is "hefty" (that is, "heavy") if its average digit value is greater than 7. The number 6959 is "hefty" because: (6 + 9 + 5 + 9) / 4 = 7.5 The number ...
9
votes
3answers
624 views

Solving triangles with trigonometry

Time to dig up your old trigonometry notes from high school! The challenge is to solve the unknown sides and angles of different triangles. And as is customary in code golf, the smallest working code ...
8
votes
10answers
476 views

Code-Golf: The Last digit

Challenge In this task you would be given A (Less than 10000 digits long) and B (less than 2^64) and you need to compute the last digit of (A · A · A · ... · A (B ...
8
votes
5answers
554 views

Egyptian Fractions

Overview: From Wikipedia: An Egyptian fraction is the sum of distinct unit fractions. That is, each fraction in the expression has a numerator equal to 1 and a denominator that is a positive integer, ...
8
votes
5answers
418 views

Circle Through Three Points

Given the Cartesian coordinates of three points on a plane, find the equation of the circle through them all. The three points will not be on a straight line. Each line of input to your program will ...
8
votes
2answers
347 views

Build a small and balanced mobile

You are given a bunch of weights, and your task is to build a small balanced mobile using those weights. The input is a list of integer weights in the range 1 through 9, inclusive. There may be ...
7
votes
13answers
885 views

Rosetta Stone Challenge: Find the rule for a series

The goal of a Rosetta Stone Challenge is to write solutions in as many languages as possible. Show off your programming multilingualism! The Challenge Your challenge is to implement a program that ...
7
votes
17answers
1k 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 ...
7
votes
11answers
533 views

Product of Digits

For a given positive integer number N, write a complete program to find the minimal natural M such that the product of digits of M is equal N. N is less than 1,000,000,000. If no M exists, print -1. ...
7
votes
11answers
777 views

Code golf: Distributing the balls (I)

Challenge In this task you have compute the number of ways we can distribute A balls into B cells with with every cell having at-least one ball. The inputs A and B are given in a single ...
7
votes
4answers
307 views

Calculate an integer sequence derived from prime factors

Create a function, expression, or program which does the following: Take the prime factors of any number and sum them. For example, the prime factors of 28 are 2 2 7, summed to 11. Multiply the ...
7
votes
6answers
293 views

Print the sizes of intervals inside of a piece of music

Background In Western music, every single music note has an assigned name. Within each octave, there are twelve unique notes in the following order: "C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B C", ...
7
votes
3answers
269 views

Find all Super Powers

A positive number is said to be super power when it is the power of at least two different positive integers. Write a program that lists all super powers in the interval [1, 264) (i.e. between 1 ...
7
votes
3answers
445 views

Implement a Graphing Calculator

There have been many questions involving calculators; however, it does not appear that any involve implementing a graphing calculator. The Challenge You are to write a complete program that takes ...
7
votes
5answers
635 views

Output a list of all rational numbers

Out of all of mathematics, there will always be a few theorems that go beyond all common sense. One of these is the fact that there are different sizes of infinity. Another interesting fact is the ...
7
votes
6answers
464 views

Find the Pisano Period of a number

Input: An integer 0 < n < 2^30, taken from stdin. Output: The Pisano period of n (the length of the cycle of the Fibonacci sequence mod n) Input is smaller than 2^30 so that intermediate ...
7
votes
3answers
248 views

Permutation group operation

There is a well-known bijection between the permutations of n elements and the numbers 0 to n!-1 such that the lexicographic ordering of the permutations and the corresponding numbers is the same. For ...
7
votes
5answers
320 views

Bijection between binary strings and pairs thereof

Input: Either one or two strings of '0's and '1's. If there are 2, they are separated by a space. All strings are of length at least 1. Output: If one string was input, 2 are output. If 2 were ...
7
votes
2answers
482 views

Break the broken cipher

I have designed a simple random generator that cycles two numbers in a chaotic way using a multiply and modulus method. It works great for that. If I were to use it as a cipher generator it would ...
7
votes
3answers
263 views

Rosetta Stone Challenge: Draw a Box Plot

The goal of a Rosetta Stone Challenge is to write solutions in as many languages as possible. Show off your programming multilingualism! The Challenge Your challenge is to implement a program that ...
7
votes
3answers
1k views

Convert infix expressions to postfix notation

When I saw the title of this closed question, I thought it looked like an interesting code golf challenge. So let me present it as such: Challenge: Write a program, expression or subroutine which, ...
7
votes
1answer
278 views

Solving Three Open Problems with a Halting Oracle

You are given the functions: h1(f,*args) and h2(f,*args) Both are methods which are already defined for you (here the asterisk indicates a variable number of arguments) f is a function, *args is a ...
7
votes
2answers
192 views

Find real roots of a polynomial

Write a self-contained program which when given a polynomial and a bound will find all real roots of that polynomial to an absolute error not exceeding the bound. Constraints I know that Mathematica ...
6
votes
15answers
505 views

Triangle Area Side Side Side

Given three sides of a triangle, print area of this triangle. Test cases: In: 2,3,4 Out: 2.90473750965556 In: 3,4,5 Out: 6 Assume the three side a,b,c always a>0,b>0,c>0,a+b>c,b+c>a,c+a>b.
6
votes
6answers
441 views

What is the average of n, the closest prime to n, the square of n and the closest fibbonacci 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 ...
6
votes
4answers
373 views

Display a bézier curve in the browser

The goal is to display a bézier curve in any classical browser (except maybe old IE versions, because... are they really browsers?) client side. Rules: You must provide one or more files (or their ...
6
votes
7answers
472 views

Code-Golf: Farey sequence (I)

Challenge In this task you would be given an integer N (less than 10^5), output the Farey sequence of order N. The input N is given in a single line,the inputs are terminated by EOF. Input ...
6
votes
5answers
218 views

Gregorian to Noxu Time System Conversion

The world's changing to a new time system! We need to update our computer programs. Given an input of epoch time (number of seconds since January 1st, 1970 00:00), print out a time in the new Noxu ...
6
votes
3answers
381 views

Calculate the number of topologies on {1,2,…,n}

Task Write a function/program which takes n as a parameter/input and prints/returns the number of topologies (which is demonstrated below) on the set {1,2,...,n}. Definition of Topology Let X ...

1 2 3