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
10 votes
13 answers
876 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. ...
user avatar
  • 4,443
18 votes
4 answers
1k 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 ...
user avatar
5 votes
4 answers
2k views

Amicable number calculator [closed]

Write a program that calculates amicable numbers, starting from [0,0], intil the maximum value for an integer is reached in the language you chose to write it in. Amicable numbers are two different ...
user avatar
14 votes
4 answers
4k 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 ...
user avatar
  • 4,443
10 votes
8 answers
661 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 ...
user avatar
21 votes
4 answers
2k 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 ...
user avatar
0 votes
23 answers
8k views

For 2 arrays, subtract every element in one array from the position-matching element in the other [closed]

For 2 arrays each with X elements, what is the SHORTEST code to subtract every element in the second from every element in the first, 1 to 1 so, for example: A1 = [10,11,12] A2 = [1,2,3] A1 "-" A2 ...
user avatar
15 votes
5 answers
3k views

Long multiply, 8 bits at a time

You are given a 16-bit machine and told to implement multiplication of arbitrary-sized integers. Your registers can only hold 16-bit numbers, and the biggest multiply instruction takes two 8-bit ...
user avatar
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 ...
user avatar
  • 471
5 votes
4 answers
775 views

Sum of Fibonacci numbers

I found this task here. Given the ith (1<=i<=35) Fibonacci number F(i) calculate the sum of the ith till i+9th number F(i)+F(i+1)+...+F(i+9) and the last digit of the i+246th one F(i+...
user avatar
  • 2,299
8 votes
3 answers
626 views

Find all Super Powers [duplicate]

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 (...
user avatar
  • 4,443
5 votes
4 answers
2k views

Fastest algorithm for decomposing a perfect power

A perfect power is a positive integer that's equal to one positive integer, to the power of another positive integer (e.g. ab is a perfect power). Note that trivial solutions (e.g. b=1) are excluded; ...
user avatar
  • 2,299
15 votes
19 answers
2k 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, ...
user avatar
  • 14.9k
8 votes
5 answers
1k views

Code-Challenge: Farey sequence (II)

Challenge In this task you would be given an integer N (less than 10^6), output the number of terms in the Farey sequence of order N. The input N < 106 is given in a single line, the inputs ...
user avatar
  • 2,299
13 votes
12 answers
1k 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 ...
user avatar
  • 2,299
12 votes
13 answers
1k 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 <...
user avatar
  • 2,299
14 votes
11 answers
895 views

The last digit in an exponentation

In this task you will be given A (less than 10000 digits long) and B (less than 2^64), and you will need to compute the last digit of (A · A · A · ... · A (B times)). The ...
user avatar
  • 2,299
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 ...
user avatar
16 votes
5 answers
6k views

Count number of hefty decimals between 2 numbers [closed]

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 ...
user avatar
12 votes
5 answers
1k views

Polynomial Interpolation

Write a program that performs Polynomial Interpolation using true arbitrary precision rational numbers. The input looks like this: f(1) = 2/3 f(2) = 4/5 f(3) = 6/7 ... You may assume that there's ...
user avatar
  • 9,970
18 votes
11 answers
3k views

Decompose a permutation into cycles

There is a well-known theorem that any permutation can be decomposed into a set of cycles. Your job is to write the shortest possible program to do so. Input: Two lines. The first contains a ...
user avatar
9 votes
2 answers
1k views

Floating point addition, without floats!

Your task is to write a program, in any language, that adds two floating point numbers together WITHOUT using any fractional or floating point maths. Integer maths is allowed. Format The format for ...
user avatar
  • 1,103
15 votes
13 answers
3k views

Compute the first N digits of e

Challenge Write a program to compute the the first N (<= 10^3) digits of e. Your program should take an integer N as input. Input: 100 Output: ...
user avatar
  • 2,299
8 votes
4 answers
441 views

The fourth power (Juzuk) [closed]

In 1939 Juzuk described a way to generate the fourth powers of natural numbers. Group the natural numbers like this: ...
user avatar
  • 2,140
18 votes
4 answers
615 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 ...
user avatar
  • 42.8k
30 votes
35 answers
12k 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 ...
user avatar
8 votes
2 answers
678 views

GolfScript - The missing function

The GolfScript language has one serious lack, no float or fixed point handling. Remedy this by creating a function F for converting an integer number, given as a string, to a string representing 1 / ...
user avatar
  • 4,395
6 votes
8 answers
813 views

Code Golf: Number of paths!

Challenge Consider a square matrix of order N(N rows and N columns). At each step you can move one step to the right or one step to the top. How many possibilities are to reach (N,N) from ...
user avatar
  • 2,299
5 votes
6 answers
394 views

Rotate Cartesian coordinates.

Write a program rotates some Cartesian coordinates through an angle about the origin (0.0,0.0). The angle and coordinates will be read from a single line of stdin in the following format: ...
user avatar
  • 693
12 votes
1 answer
854 views

Break the broken cipher [closed]

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 ...
user avatar
  • 4,395
10 votes
20 answers
951 views

Calculate probability of getting half as many heads as coin tosses.

Write a program that, given a small positive even integer from standard input, calculates the probability that flipping that many coins will result in half as many heads. For example, given 2 coins ...
user avatar
  • 693
25 votes
19 answers
4k 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 ...
user avatar
  • 675
8 votes
13 answers
1k views

How many switches are ON

A class has N student and the class room has N switches (all turned OFF and numbered 1 to N). Each student has an enrollment number assigned from 1 to N. Each student serially (by number) toggles the ...
user avatar
13 votes
3 answers
577 views

Physics golf: inclined shooting

An angry bird is shot at an angle \$β\$ to the horizontal at a speed \$u\$. The ground is steep, inclined at an angle \$α\$. Find the horizontal distance \$q\$ that the bird traveled before it hit the ...
user avatar
  • 5,438
5 votes
7 answers
900 views

Modulo code golf

Write a function f(a,b) that returns a mod b given the constraints: you can't use division in any way ...
user avatar
  • 5,438
4 votes
3 answers
716 views

Implement BCD Arithmetic

Write the shortest function you can that takes two binary-coded decimal integers and a character indicating an arithmetic operation to perform and return the result. Input Specification You will be ...
user avatar
  • 2,022
15 votes
5 answers
2k views

Polynomial Long Division

Implement polynomial long division, an algorithm that divides two polynomials and gets the quotient and remainder: (12x^3 - 5x^2 + 3x - 1) / (x^2 - 5) = 12x - 5 R 63x - 26 In your programs, you will ...
user avatar
  • 5,871
15 votes
4 answers
3k views

Roman numeral converter function

Create the shortest function to convert a string of Roman numerals to an integer. The rules for each letter can be found at the Wikipedia page. Letters above 1,000 will have parentheses placed ...
user avatar
24 votes
17 answers
5k views

Last non-zero digit of n!

Given an integer 1 ≤ N ≤ 1,000,000 as input, output the last non-zero digit of N!, where ! is the factorial (the product of all numbers from 1 to N, inclusive). This is OEIS sequence A008904. Your ...
user avatar
  • 4,443
7 votes
17 answers
3k views

Multiply without multiply [closed]

Write the fastest (best big-O) and smallest multiplication algorithm for positive integers, without using multiplication operators. You are only allowed addition, subtraction, logical functions (AND, ...
user avatar
  • 3,262
29 votes
5 answers
2k 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 ...
user avatar
21 votes
19 answers
4k views

Basic Calculator

You must write a program to evaluate a string that would be entered into a calculator. The program must accept input and output the correct answer. For languages that do not have standard input/...
user avatar
13 votes
5 answers
632 views

Perfect powers in more than one way?

Challenge Your task is to write a program or function which, given a positive integer \$N\$, finds all positive integers less than or equal to \$N\$ that can be expressed as a perfect power in more ...
user avatar
  • 4,443
2 votes
1 answer
117 views

Five aligned points and some of the pairwise distances. Infer the relative positions! [closed]

Given five points on a straight line such that their pairwise distances are 1,2,4, ..., 14,18,20 (after ordering), find the respective positions of the five points (relative to the furthest point on ...
user avatar
  • 324
44 votes
43 answers
10k 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 ...
user avatar
18 votes
12 answers
864 views

Compute modular inverse

Given two positive numbers \$x\$ and \$n\$ with \$x<2^n\$, write the shortest possible function to compute \$x^{-1} \mod 2^n\$. In other words, find \$y\$ such that \$xy=1 \mod 2^n\$. Your ...
user avatar
12 votes
2 answers
3k views

Test if a given number is a Vampire Number [duplicate]

EDIT: In the interest of increasing the complexity, i've added more to the challenge. In mathematics, a vampire number (or true vampire number) is a composite natural number v, with an even ...
user avatar
  • 2,140
4 votes
3 answers
262 views

Sharing Inherited Land [closed]

You and your brother have inherited a rectanglar block of land. This block of land's corners are read in from stdio as TLX,TLY,TRX,TRY,BLX,BLY,BRX,BRY (all decimal ...
user avatar
  • 1,009
24 votes
38 answers
4k views

Reimplementing square root [duplicate]

Define a function, s, which takes a number and returns the square root. No use of library functions, such as Java's Math.sqrt() or PHP's built in sqrt(), allowed.
user avatar
  • 1,145
39 votes
21 answers
7k views

Angle between the hands on a clock [duplicate]

Given the time in 24 hour format (2359 = 11:59pm) return the angle between the minute and hour hands on a standard clock (on the ...
user avatar
  • 1,009