Questions related to elementary arithmetic.

learn more… | top users | synonyms

22
votes
17answers
2k views

Construct a Ladder

Introduction I want to build a ladder. For this, I have scavenged from the junkyard two long boards with holes in them, and I want to place the steps into these holes. However, the holes are not ...
14
votes
16answers
2k views

Different Way Forward

Given a list of integers produce a Forward Difference at a specified order/depth. For the list of integers: (10, 18, -12, 4, 8, -3, -5, 67, 9, 14) The Forward Differences at the various ...
10
votes
12answers
988 views

Apportionment paradox

Given: A natural number S. A list of N rational weights W that sum to 1. Return a list L of N non-negative integers, such that: (1) sum(L) = S (2) sum((S⋅W_i - L_i)^2) is minimal In other ...
10
votes
5answers
749 views

How many ways to write N as a product of M integers?

Given an integer N, count how many ways it can be expressed as a product of M integers > 1. Input is simply N and M, and output is the total count of distinct integer groups. Meaning you can use an ...
-1
votes
6answers
132 views

Double Factorial [duplicate]

Challenge The objective is simple, in the shortest amount of bytes, write a function or program that takes a number and prints its double factorial. The input must be taken from stdin or a function ...
14
votes
7answers
615 views

Sum the powers to n

Directions Write a program that, given an input integer n (n >= 0), outputs the smallest positive integer m where: n = a[1]^b[1] + a[2]^b[2] + a[3]^b[3] + ... + a[k]^b[k] a and b are finite ...
8
votes
2answers
325 views

Hereditary Base Change

Background In this challenge, a base-b representation of an integer n is an expression of n as a sum of powers of b, where each term occurs at most b-1 times. For example, the base-4 representation ...
9
votes
36answers
2k views

Calculate the square of an integer with restrictions [closed]

The Puzzle You have to write a program or function p(n) that returns the square of what is entered into it and you may assume that the input is a non-negative integer. Put in simpler terms, return ...
15
votes
5answers
741 views

Make any number by repeatedly adding 2 numbers

You are given a machine with two 16-bit registers, x and y. The registers are initialized x=1 and y=0. The only operation the machine can do is addition modulo 65536. That is: x+=y - x is replaced ...
18
votes
9answers
846 views

Sum the time durations

Challenge Write the shortest code that can sum all the time durations that appear in the stdin. The program must only consider the strings that match with one of the following patterns and ignore the ...
5
votes
2answers
697 views

Coding a recursive function for highest possible input

Challenge You are given the following function:- which is the same as:- with the base cases q(r, b, L) = 1 whenever r ≤ L, q(r, 0, L) = 0, if r > L and q(r, 0, L) = 1, if r ≤ L. Your task is to ...
7
votes
2answers
594 views

Divide food to the poor equally!

The problem Through a terrible procastination accident you are reborn as Scrooge McDuck for a day. To make the most out of the situation you decide to give away food to the poor. Since you also are a ...
16
votes
10answers
2k views

Undo the square roots

Your job is to convert decimals back into the sum of the square roots of integers. The result has to have an accuracy of at least 6 significant decimal digits. Input: A number indicating the number ...
9
votes
15answers
2k views

Sum of strings without converting

You have to take 2 string inputs and output the sum of these two strings without converting them to int or using any numeric data type. ex. string one = "123"; string two = "456"; string sum = ...
3
votes
8answers
924 views

Wait, but what's e^N?

Objective: The objective is to calculate e^N for some real or imaginary N (e.g. 2, -3i, 0.7, 2.5i, but not 3+2i). This is code golf, so, shortest code (in bytes) wins. So, for example: N = 3, e^N ...
11
votes
5answers
395 views

Generalized Gematria Calculator

Create a bidirectional Gematria calculator, for any given sequence of Unicode characters as the alphabet. Gematri-What? Gematria is a system of assigning numerical values to symbols, developed by ...
13
votes
25answers
3k views

Find i^n, given n

The Challenge In as few characters as possible, find the value of i^n, given n, a positive integer greater than 0. This should be outputted as a String. For those that don't know, i is defined such ...
1
vote
0answers
266 views

Implement multiplication without using any loops, branches or function calls [closed]

I came up with the following in GNU C r=0;m(a,b){x:goto*(int*[]){&&x,&&y}[r+=a&1?0:b,a>>=1,b<<=1,a<1];y:;} One could argue there is a branch here a&1?0:b So ...
32
votes
8answers
2k views

How Many Guineas in a Gross of Threepennies?

Until decimalization in 1971, British money was based on dividing the pound into 240 pennies. A shilling was 12 pennies so 20 shillings made a pound. The smallest denomination was the farthing at one ...
20
votes
16answers
3k views

Multiply two numbers without using any numbers

You are given as input two strings representing positive integers in base 10, such as "12345" and "42". Your task is to output a string containing their product, "518490" in this case. The twist is ...
20
votes
14answers
2k views

un-round fractions

When you convert a fraction a decimal number and you want to store that number, you often have to round it, because you only want to use a certain amount of memory. Lets say you can only store 5 ...
1
vote
2answers
174 views

Distribution of Variables [duplicate]

I was annoyed the other day when I got a point off in math class because I didn't distribute the factors of my function. That is, I put (x+2)(x-3) when I should have put x^2 - x - 6. Your job is to ...
11
votes
5answers
1k views

It’s time to do the math

Introduction This is one of my favorite math puzzles. Given a digit (say 3) and the number of times to use that digit (say 5), generate 10 expressions which result to 1, 2, 3, 4, 5, 6, 7, 8, 9 and ...
7
votes
6answers
642 views

Mixed Base Conversion

Background Most people on here should be familiar with several base systems: decimal, binary, hexadecimal, octal. E.g. in the hexadecimal system, the number 1234516 would represent 1*16^4 + 2*16^3 + ...
25
votes
1answer
669 views

Standardise a Phinary Number

Background Most people on here should be familiar with a few integer base systems: decimal, binary, hexadecimal, octal. E.g. in the hexadecimal system, a number abc.de16 would represent a*16^2 + ...
11
votes
6answers
413 views

False Positives on an Integer Lattice

Leaderboard User Language Score ========================================= Ell C++11 293,619,555 feersum C++11 100,993,667 Ell ...
16
votes
4answers
1k views

Multiply with restricted operations

Goal Your goal is to multiply two numbers using only a very limited set of arithmetic operations and variable assignment. Addition x,y -> x+y Reciprocal x -> 1/x (not division x,y -> ...
10
votes
6answers
991 views

Implement an 8 bit adder

The Challenge Implement a function which accepts two integers whose values range from 0 - 255 and returns the sum of those integers mod 256. You may only use bitwise negation (~), bitwise or (|), bit ...
12
votes
7answers
486 views

Find the maximum operation

The challenge is to find the maximum number you can get from a list of integer using basic arithmetic operators (addition, substraction, multiplication, unary negation) Input A list of integers ...
3
votes
6answers
1k views

Solve quadratic equation using only 3 variables and arithmetic

Write function which solves equation of degree less than or equal to 2 (ax^2 + bx + c = 0). Input Function should get 3 parameters - floating point numbers which are coefficients of this equation. ...
7
votes
1answer
229 views

Minimal cover of bases for quadratic residue testing of squareness

Challenge Find the smallest cover of bases (e.g., moduli) whose sets of quadratic residue can be tested via table-lookup to definitively determine whether or not a given non-negative integer n is a ...
9
votes
4answers
474 views

Kakuro Combinations

Kakuro Combinations Because I can't do mental arithmetic, I often struggle with the Kakuro Puzzle, which requires the victim to repeatedly work out which distinct numbers in the range 1 to 9 ...
1
vote
5answers
404 views

Modular exponentiation using only addition and subtraction

The challenge In the least number of source code characters, in a language of your choosing, devise a function which raises base a to power b, modulo m, using purely integer arithmetic, where a, b, ...
11
votes
28answers
2k views

Integer square root of integer

Problem: In your choice of language, write the shortest function that returns the floor of the square root of an unsigned 64-bit integer. Test cases: Your function must work correctly for all ...
3
votes
14answers
732 views

Base-2 integer logarithm of 64-bit unsigned integer

Problem: In your choice of language, write the shortest function that returns the floor of the base-2 logarithm of an unsigned 64-bit integer, or –1 if passed a 0. (Note: This means the return type ...
-1
votes
2answers
242 views

Shortest test for primality for byte [duplicate]

Write a function which gets as input a number (0 <= n <= 255) stored in 32-bit variable and returns non-zero integer if n is prime number and 0 otherwise. Considern = 0 and n = 1 as non-primes. ...
19
votes
5answers
4k views

Shortest expression for {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}

Given list of integers {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}. For those who interested these numbers are used in weekday calculation. Weekday = (m[n] + d + y + y>>2 + y/400 - y/100) % 7;, where ...
3
votes
4answers
429 views

Difference of expressions when evaluated in different orders

We all know the standard order of operations for math functions (PEMDAS), but what if we instead evaluated expressions left-to-right? The Challenge Given a string through standard input or through ...
7
votes
12answers
1k views

Generate all possible integers

Input: you are given 2 integers, n and k. n is a positive (>= 1) integer, and k an integer, which is the number of times you are allowed to use n. Output: output all of the non-negative integers ...
14
votes
17answers
2k views

Length of String Using Set Theory

From Wikipedia Set-theoretic definition of natural numbers The set N of natural numbers is defined as the smallest set containing 0 and closed under the successor function S defined by S(n) = n ...
1
vote
0answers
95 views

Give all the ways to reach 100 [duplicate]

Write a program that finds all the ways you can write 100 as 123456789 separated only by the characters +-*/ and space. Example: 123 + 4 - 5 + 67 - 89 = 100. You can output your answers in any format ...
6
votes
10answers
363 views

how to find n'th number in the decimal expansion of a proper fraction?

Given three integers N,D,R write a program to output Rth digit after the decimal point in the decimal expansion for a given proper fraction N/D.
5
votes
2answers
490 views

math with bitwise operators

objective write a program that can perform addition, subtraction, multiplication, and division, using only bit-wise operators, by bit-wise operators i mean the equivalent of JavaScript &, |, ^, ...
0
votes
3answers
2k views

javascript: smallest code to convert seconds to time [closed]

Given seconds like 11728, write the smallest javascript function that returns a string like 3hrs. 15min. 28sec. signature textTime([int] t,[bool] z,[bool] m) t is the time like 11728 and you can ...
10
votes
4answers
983 views

No branching please

Anyone who is moderately into low level code optimization know about the perils of branching, be it implemented as if-statements, loops or select-statements the possibility of a branch misprediction ...
2
votes
1answer
604 views

Basic Calculator with GUI

I recently had to add a small basic calculator to a project. I wrote a quick dirty one, but it seemed small enough to check with you people out here ;) Just out of curiosity & an appetite to get ...
6
votes
3answers
378 views

Simultaneous equations

With equations that have more than one unknown, simultaneous equations can be used in solving for them. This challenge will only deal with linear equations with two unknowns, to make things as ...
5
votes
5answers
888 views

Build a solver for the cow and chicken problem

The cow and chicken problem is a traditional problem for introducing young students to the concept of systems of equations. It goes as follows: A farmer raises cows and chickens on his farm. His ...
16
votes
3answers
1k views

Four fours puzzle

The Four fours puzzle is a popular recreational mathematical puzzle that involves using exactly four 4s (and no other number) and a defined set of operations to reach every number from 0 to a given ...
7
votes
4answers
335 views

Fourth of July Edition: Print lines of a file containing multiples of a specific number

Create a program which takes one command-line argument, n, which will be an integer less than 2147483648 (2^31), and then reads a file input.txt and prints the lines of input.txt which contain any ...