Questions tagged [integer]

For challenges involving the manipulation of integers.

102
votes
32answers
11k views

What are the five most powerful characters in your language?

Choose any five characters your language supports. There are 5! = 5×4×3×2×1 = 120 ways these can be arranged into a 5-character string that contains each character once; 120 ...
66
votes
141answers
7k views

Output the sign

Given a number N, output the sign of N: If N is positive, output 1 If N is negative, output -1 If N is 0, output 0 N will be an integer within the representable range of integers in your chosen ...
66
votes
5answers
5k views

Powerprogramming: O(1^N), O(N^1), O(2^N), O(N^2) all in one

Write a program (or function) that exhibits four common big O time complexities depending on how it is run. In any form it takes in a positive integer N which you may assume is less than 231. When ...
62
votes
47answers
6k views

N-dimensional N^N array filled with N

In: Enough memory and a positive integer N Out: N-dimensional N^N array filled with N, where N^N means N terms of N-by-N-by-N-by... Examples: 1: [1] ...
57
votes
18answers
6k views

Count like Chuck Norris

As is well known, Chuck Norris counted to infinity. Twice Besides, Chuck Norris can count to infinity backwards. Also, although perhaps less known, Chuck Norris can speak a little Spanish in ...
50
votes
42answers
3k views

Illustrate the Least Common Multiple

Given two positive integers, A and B, illustrate their least common multiple by outputting two lines of dashes (-) with length LCM(A, B) after replacing every Ath ...
48
votes
120answers
7k views

Print all integers

Write a program or function which will provably print all integers exactly once given infinite time and memory. Possible outputs could be: ...
45
votes
20answers
4k views

Greater than less than greater than something fishy

Given a length N string of less-than and greater-than signs (<, >), insert the integers 0 through N at the start and end ...
44
votes
50answers
3k views

Output integers in negative order, increase the maximum integer everytime

Main task Your task is to print out integers in descending order, starting from 1, and increasing as you keep hitting 1 again, up until the given input is reached, then, print out the rest until you ...
44
votes
26answers
2k views

Divisor skyline

For any positive integer k, let d(k) denote the number of divisors of k. For example, ...
44
votes
37answers
3k views

Increment an Array

Given a nonempty array of positive integers, "increment" it once as follows: If all the array elements are equal, append a 1 to the end of the array. For example: ...
42
votes
16answers
4k views

How small can it get?

Starting with a positive integer N, find the smallest integer N' which can be computed by repeatedly dividing N by one of its digits (in base-10). Each selected digit must be a divisor of N greater ...
41
votes
40answers
7k views

Is this number an integer power of -2?

There are clever ways of determining whether a number is a power of 2. That's no longer an interesting problem, so let's determine whether a given integer is an integer power of -2. For example: <...
41
votes
39answers
2k views

An Array of Challenges #1: Alternating Arrays

Alternating Arrays An alternating array is a list of any length in which two (not necessarily different) values are alternating. That is to say, all even-indexed items are equal, and all odd-indexed ...
40
votes
118answers
8k views

Stay away from zero

Task Given a non-negative integer n, output 1 if n is 0,...
40
votes
31answers
2k views

Moving modest minimum

Inspired by a question over at Stack Overflow. The title here is entirely my fault. The challenge Given a list of positive integers containing at least two entries, replace each number by the ...
38
votes
32answers
6k views

Never tell me the odds

Input A integer n ( ≥ 1) and a digit d (an integer such that 0 ≤ d ≤ 9). In either order; from stdin or parameters or whatever; to a program or function; etc. ...
36
votes
26answers
5k views

Oddly Even, Positively Negative

Given N, output the Nth term of this infinite sequence: -1 2 -2 1 -3 4 -4 3 -5 6 -6 5 -7 8 -8 7 -9 10 -10 9 -11 12 -12 11 ... etc. N may be 0-indexed or 1-...
36
votes
24answers
3k views

Complex Binary Numbers

Let's create a simple, surjective mapping from positive integers to Gaussian integers, which are complex numbers where the real and imaginary parts are integers. Given a positive integer, for example ...
36
votes
14answers
1k views

An Array of Challenges #2: Separate a Nested Array

Note: This is #2 in a series of array-manipulation challenges. For the previous challenge, click here. Separating Nested Lists To separate values in a nested list, flatten it, and then wrap each ...
35
votes
71answers
4k views

As Easy as One-Two-Three

Write a program or function that takes in a positive integer. You can assume the input is valid and may take it as a string. If the number is any of ...
35
votes
34answers
3k views

Is it a Mersenne Prime?

A number is a Mersenne Prime if it is both prime and can be written in the form 2n-1, where n is a positive integer. Your task is to, given any positive integer, determine whether or not it is a ...
35
votes
20answers
2k views

Sum the powers that be

A simple but hopefully not quite trivial challenge: Write a program or function that adds up the kth powers dividing a number n....
35
votes
11answers
1k views

Prime parity peregrination

The purpose of this challenge is to graphically depict a walk on the plane, where the direction of each step \$k\$ is determined by the primality of \$k\$ and the parity of its binary expansion: ...
34
votes
64answers
4k views

Output the missing integer

You will be given a string. It will contain 9 unique integers from 0-9. You must return the missing integer. The string will look like this: ...
34
votes
65answers
3k views

Interleave numbers from 1 to n, with the same numbers reversed

A simple one: Take a positive integer n less than 1000, and output the integers from 1 to n interleaved with the integers from n to 1. You must concatenate the numbers so that they appear without any ...
34
votes
42answers
3k views

Up and down, up and down

Challenge: Given a positive integer input n, create a vector that follows this pattern: 0 1 0 -1 -2 -1 0 1 2 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 ... ±(n-1) ±n ...
34
votes
35answers
2k views

Undo a Range of Numbers

It is fairly simple to, given a number n, create a range from 0 to n-1. In fact, many ...
34
votes
14answers
1k views

Generate n-ary numbers

A secondary number is a positive integer whose prime factors (without multiplicity) are all less than or equal to its square root. 4 is a secondary number, because ...
33
votes
61answers
5k views

Find the highest unique digit

Surprisingly we haven't had a simple "find the highest digit" challenge yet, but I think that's a little too trivial. Given input of a non-negative integer, return the highest unique (ie not repeated)...
33
votes
41answers
5k views

Half, Half Half, and, Half

Consider the following number sequence: \$ 0, \frac{1}{2}, \frac{1}{4}, \frac{3}{4}, \frac{1}{8}, \frac{3}{8}, \frac{5}{8}, \frac{7}{8}, \frac{1}{16}, \frac{3}{16}, \frac{5}{16}, \frac{7}{16}, \...
33
votes
57answers
6k views

Is my barcode valid?

An EAN-8 barcode includes 7 digits of information and an 8th checksum digit. The checksum is calculated by multiplying the digits by 3 and 1 alternately, adding the results, and subtracting from the ...
33
votes
31answers
3k views

Let's decrease the monotony

...but hey, no need to be strict. Given a non-empty array of strictly positive integers, determine if it is: Monotone strictly decreasing. This means that each entry is strictly less than the ...
33
votes
32answers
4k views

Reduce a number by its largest digit

Task: Given an integer number in decimal number system, reduce it to a single decimal digit as follows: Convert the number to a list of decimal digits. Find the largest digit, D Remove D from the ...
33
votes
30answers
6k views

Find the smallest positive integer which ends in n, is divisible by n and whose digits sum to n

It's all in the title... Take as input a positive integer n>=12 and... do what the title says. Yes, this is on OEIS A187924. Some test cases ...
33
votes
8answers
914 views

Can square tree rings be generated from primes?

Apparently yes! In three easy steps. Step 1 Let f(n) denote the prime-counting function (number of primes less than or equal to n). Define the integer sequence s(n) as follows. For each positive ...
33
votes
5answers
8k views

Historical difference between `/` and `÷` in mathematical expressions

Introduction: Inspired by a discussion that is already going on for many years regarding the expression \$6÷2(1+2)\$. With the expression \$6÷2(1+2)\$, mathematicians will quickly see that ...
32
votes
33answers
2k views

Digits in their lanes

Input: A list of integers Output: Put each digit (and the minus sign) in its own lane, in the order -0123456789, ignoring any duplicated digits. Example: Input:...
32
votes
21answers
2k views

A ​Note ​on ​N!

J. E. Maxfield proved following theorem (see DOI: 10.2307/2688966): If \$A\$ is any positive integer having \$m\$ digits, there exists a positive integer \$N\$ such that the first \$m\$ digits of \$...
32
votes
4answers
4k views

The happy Ender problem

The happy ending problem (actually a theorem) states that Any set of five points in the plane in general position has a subset of four points that form the vertices of a convex quadrilateral. The ...
31
votes
42answers
2k views

Least Common Multiple

The least common multiple of a set of positive integers A is the smallest postive integer B such that, for each ...
31
votes
34answers
4k views

Make a Spiky Box

Given two positive integers, W and H, output an ASCII-art box whose border is made of slashes (/ and \) with W "spikes" on the ...
30
votes
47answers
4k views

Which Card is it?

Introduction Long ago, when I used to code card games with usual playing cards, I used to specify a number for each card and call a function with some number to get a card. This somewhat inspired me ...
30
votes
69answers
8k views

Check whether an integer is a power of 2 without using +,- operations [closed]

Write a program that checks if the integer is a power of 2. Sample input: 8 Sample output: Yes Sample input: ...
30
votes
23answers
8k views

Could you please stop shuffling the deck and play already?

Challenge: Input: A list of distinct positive integers within the range \$[1, \text{list-size}]\$. Output: An integer: the amount of times the list is riffle-shuffled. For a list, this means the ...
30
votes
38answers
2k views

Find the sum of all numbers below n that are a multiple of some set of numbers

Almost equivalent to Project Euler's first question: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find ...
30
votes
18answers
2k views

N-dimensional identity “matrix”

Given a positive integer n, output the N-dimensional identity "matrix", which is the N^N ...
30
votes
21answers
2k views

Find the nearest greater number

The task Given any array of integers, e.g.: [-1,476,578,27,0,1,-1,1,2] and an index of that array (this example uses 0 based indexing, though you can use 1 ...
29
votes
20answers
2k views

Float 754 to Hamming

You will be given as input an integer k in the range from -4503599627370496 (−252) to ...
29
votes
43answers
2k views

The Squaring Sequence

Each term in the squaring sequence, xn, is created by taking xn-1, squaring it, and removing all but the first four digits. The sequence always begins with x1 = 1111. Squaring this yields 1234321, ...