Questions tagged [integer]
For challenges involving the manipulation of integers.
461 questions
18
votes
8
answers
1k
views
English to Dutch number order conversion
Introduction:
When saying numbers out loud, the order is slightly different in Dutch than in English. For a number like \$1234\$:
in English you'd say: one thousand, two hundred and thirty-four
in ...
15
votes
18
answers
1k
views
Rebase Until Inert
This is a sequel to Numbers Interpreted in Smallest Valid Base.
Let us define the operation of rebasing a number as writing out its digits in decimal form, then interpreting them in the smallest base <...
20
votes
31
answers
2k
views
Numbers Interpreted in Smallest Valid Base
Let us define the operation of rebasing a number as writing out its digits in decimal form, then interpreting them in the smallest base N possible (...
12
votes
12
answers
743
views
Recursive cumulative sum [duplicate]
Challenge:
Given inputs \$i\$ and \$n\$, calculate \$R_i(n)\$ where:
$$R_0(n)=n \\
R_i(n)=\sum_{j=0}^nR_{i-1}(j)$$
Note that \$R_1\$ is triangular function, and \$R_2\$ is tetrahedral function.
This ...
10
votes
2
answers
368
views
Check if a number is a power with its program value being a power
Task:
Your task is make a program to check if there exists integers \$a \ge 2, b \ge 2\$ such that \$a^b=i\$ where \$i\$ is the input integer at least 2.
The catch is that your program must return a ...
11
votes
11
answers
784
views
Find a linear function such that if f(0), f(1), f(2), ⋯ are indices of the elements of the list, the sum must equal to its slope
Challenge:
Find integers \$a\$ and \$b\$ defining linear function \$f(x) = ax + b\$ with \$a > 0, b \ge 0\$ such that the sum of the elements of the input list \$l\$ located at the values of the ...
19
votes
29
answers
2k
views
Add Y to X to get a palindrome
Given X>0, return the smallest Y>=0 such that X+Y is a palindrome. Code golf.
Tests:
5 => 0
14 => 8
200 => 2
819 => 9
1100 => 11
1122 => 99
13
votes
9
answers
2k
views
Pennies to Dollars
Over on Puzzling a couple years ago, Hermant Agarwal proposed the following question:
In a certain country the following coins are in circulation: 1 cent, 2 cents, 5 cents, 10 cents, 20 cents, 50 ...
2
votes
13
answers
676
views
List of prime distances
Task:
Write a code golf program that, given two positive integers n and m, returns a list of the distances between consecutive prime numbers in the range [n, m] ...
19
votes
12
answers
1k
views
Print Dyck paths
Definition
A Dyck path of length \$2n\$ can be defined as a two-dimensional path such that:
The path consists of straight lines of equal length.
The path goes left to right while moving either up or ...
3
votes
1
answer
299
views
Long running section 11.4 Minsky machine
Challenge
We define a Minsky machine as a program made of the following two instructions:
+X which increments the register X and continues to the next instruction
-Xn which simply continues if X is ...
4
votes
4
answers
580
views
Find the boolean logic to check if a number is prime!
Your task is to find a boolean expression that uses AND (\$\land\$), OR (\$\lor\$), XOR (\$\oplus\$), and NOT (\$\lnot\$) operators and binary digit positions (\$d_1,d_2,d_3,d_4,d_5,d_6,d_7,d_8,d_9,d_{...
-5
votes
4
answers
205
views
Determine if a rational number is an integer, without division [closed]
Determine if a rational number is also an integer, without any form of dividing. The input should be any pair of numerator and denominator. Don't forget to consider negative numbers, and division by ...
7
votes
1
answer
355
views
Solving the high school algebra problem
We learned many identities involving addition, multiplication and exponentiation in highschool, for example:
$$ \begin{aligned}
(a+b)c &= ac + bc \\
(a b)^c &= a^c b^c \\
(a^b)^c &= a^{bc}
...
16
votes
21
answers
2k
views
ZigZag encoding and decoding
ZigZag encoding is a method of encoding signed integers to unsigned integers. Non-negative integers are multiplied by two. Negative integers are equal to their absolute values multiplied by two, minus ...
33
votes
26
answers
9k
views
Print 4 billion if statements
Recently (okay, December 2023, I'm a little late) there's been a meme going around about a program checking if a 32-bit unsigned integer is even or odd using four billion if statements:
...
18
votes
11
answers
2k
views
Count squares in my pi approximation
One way to approximate π is the following: Start by drawing a 2x2 square with a quarter-circle in it. Then, the area of the quarter-circle is π.
We can approximate this area by filling it with ...
12
votes
12
answers
1k
views
Encode a VarInt
Similar question to my previous one, but in reverse. This time I want you to encode an integer into a VarInt.
Background:
I'm not going to assume everyone's seen my previous question, so here is a ...
20
votes
13
answers
3k
views
Parse Minecraft's VarInt
If you look at the reverse-engineered Minecraft Java Edition server protocol, there is an interesting integer encoding method that is used quite often called VarInt.
It works like this:
You have an ...
13
votes
27
answers
1k
views
Construct a uInt from an array of bits
Task is simple!
Take an array of binary values (can be boolean, 0/1, etc.) and use those bits to construct an n-th bit long unsigned integer (return type does not need to be an unsigned integer, just ...
16
votes
27
answers
5k
views
FizzFizzFizzBuzz!
Given a positive integer n, output the "FizzFizzFizzBuzz" sequence for that number: print the decimal representations of the numbers 1 through ...
24
votes
19
answers
2k
views
Emulate Jelly's tie-scan
The golfing language Jelly has a built-in ƭ called "tie", that cycles through a list of functions every time it's called. For example, ...
14
votes
14
answers
3k
views
Break an integer into even- and odd-indexed bits
Objective
Given an integer \$n\$ interpreted as two's complement binary, output two integers, namely the integer consisting of \$n\$'s bits at places of \$2^0, 2^2, 2^4, \cdots\$, and the integer ...
2
votes
3
answers
256
views
Rank poker High Card hands [closed]
In the poker game there are 1277 unique 'High Card' ranks. It's 1287 (13 over 5) if we include all straights.
The challenge is to write a function which returns an integer value corresponding to the ...
14
votes
7
answers
2k
views
How quickly can you type this unary string?
If I want to type the string aaa, the least keystrokes I can type it in is 3: a a a. But if I want to type the string ...
10
votes
5
answers
1k
views
Manipulate integers to zero
Objective
Given a nonempty unordered list of positive integers, decide whether they can be added, negated, or multiplied to yield a single zero. Each given number must be used exactly once.
There is a ...
19
votes
26
answers
2k
views
Is it a tetrate of two?
The tetration operation consists of repeated exponentiation, and it is written ↑↑. For instance,
3↑↑3 =3 ^(3^3) = 3^27 = 7,625,597,484,987
A tetrate of two is an ...
20
votes
25
answers
2k
views
Divmod continuously until the remainder is 1 or 0, then get the remainder
The task is simple, divide, get the quotient and the remainder, and if the remainder isn't 1 or 0, do the same thing (quotient divmod remainder) until the remainder is 1 or 0, then get the remainder. ...
13
votes
14
answers
2k
views
Counting Collinear Points
Given two points \$(x_1, y_1)\$ and \$(x_2, y_2)\$ with integer coordinates, calculate the number of integer points (excluding the given points) that lie on the straight line segment joining these two ...
4
votes
1
answer
678
views
Which line is best?
Consider an \$n \times n\$ grid of integers. The task is to draw a straight line across the grid so that the part that includes the top left corner sums to the largest number possible. Here is a ...
18
votes
39
answers
3k
views
Rudin-Shapiro sequence
The Rudin-Shapiro sequence is a sequence of \$1\$s and \$-1\$s defined as follows: \$r_n = (-1)^{u_n}\$, where \$u_n\$ is the number of occurrences of (possibly overlapping) \$11\$ in the binary ...
17
votes
23
answers
2k
views
Monotone sequence beatitude
Provided that the input is a monotone sequence of three or more integers:
Output -2 if the sequence strictly decreases. Example: [7,4,3,2]
Output -1 if the ...
11
votes
8
answers
2k
views
Add two natural numbers
Given two natural numbers (less than 100) as input print the sequence of intermediate results obtained when computing the sum of the two numbers using only the following operations1:
...
17
votes
14
answers
2k
views
Finding the power sandwich version 2
Introduction
This question is inspired by this great question.
Challenge
Given a number \$N>0\$, output the largest integer \$a^b\$ that is smaller or equal to \$N\$, and the smallest integer \$c^d\...
15
votes
16
answers
2k
views
Finding the power sandwich
Introduction
Finding the closest power to a number is a common enough problem. But what if you need both the next-highest and next-lowest power? In this challenge you must find the closest powers to a ...
6
votes
7
answers
4k
views
Help, I can't bloody well memorize this number!
The Brits aren't exactly known for being good at memorizing stuff. That's why scientists need newspaper articles to find those who are, and that's why it shouldn't be a surprise that they forget more ...
15
votes
14
answers
1k
views
The multiples are missing
Given a number \$n\$, you are to compute the sequence of positive numbers where for each number \$a\$, the \$n\$-times multiple \$n\cdot a\$ is missing.
Example
We always start with the sequence of ...
25
votes
29
answers
3k
views
Longest Consecutive Sequence
Description
Given a list of positive integers, write the shortest code to find the number count of the longest consecutive sequence of numbers within the list.
Input
A list of positive integers. You ...
23
votes
14
answers
2k
views
Sum of consecutive nth powers
Related.
Given a positive integer \$n\$, output all integers \$b\$ (such that \$1<b<n-1\$) where \$n\$ can be written as the sum of any number of consecutive powers of \$b\$.
Example:
Let's say \...
9
votes
11
answers
907
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. ...
15
votes
16
answers
997
views
Output the smallest increasing sequence where each term is coprime to preceding 3 terms
This sequence is defined as
Starts with 1, 2, 3
The next element of the sequence is the first number greater than the previous three that is co-prime with each of the previous 3 elements in the ...
23
votes
31
answers
3k
views
Is this a powerful number?
A powerful number is a positive integer \$n\$ such that for every prime \$p\$ that divides \$n\$, \$p^2\$ also divides \$n\$. Or equivalently, \$n\$ is powerful if and only if it can be written in the ...
13
votes
15
answers
2k
views
The number of solutions to Hertzsprung's Problem
Hertzprung's Problem (OEIS A002464) is the number of solutions to a variant of the Eight Queens Puzzle, where instead of placing \$n\$ queens, you place \$n\$ rook-king fairy pieces (can attack like ...
30
votes
10
answers
4k
views
Implement grambulation
Consider the following spiral of positive integers:
We now define grambulation as a binary operation \$\lozenge : \mathbb N \times \mathbb N \to \mathbb N\$, using this grid. Some example inputs and ...
23
votes
9
answers
3k
views
How many xor'ed waves?
In this challenge, you're given a list of overlapping waves. Each wave is a finite list with n truthy values, then n falsy ...
13
votes
4
answers
1k
views
Is it a Frog List?
Challenge:
A Frog List is defined by certain rules:
Each frog within a Frog List has an unique positive digit [1-9] as id (any 0...
13
votes
17
answers
1k
views
A Fine sequence with fine interpretations
The ubiquitous Catalan numbers \$C_n\$ count the number of Dyck paths, sequences of up-steps and down-steps of length \$2n\$ that start and end on a horizontal line and never go below said line. Many ...
14
votes
11
answers
885
views
Determine the Octet(/Duplet/18-electron) Rule
Given an atomic number in the inclusive range [1,118], write the shortest program/function that outputs one of {2,8,18} if the ...
22
votes
27
answers
3k
views
Sum of Consecutive Squares
Your task
Given a integer input, \$ n \$ (such that \$ n > 1 \$), decide whether it can be written as the sum of (at least 2) consecutive square numbers.
Test cases
Truthy:
...
22
votes
17
answers
3k
views
Don't repeat yourself
In this challenge you will be tasked with implementing a sequence of natural numbers such that:
Each number appears a natural number of times
No two numbers appear the same number of times
No two ...