Questions tagged [code-golf]

Code-golf is a competition to solve a particular problem in the fewest bytes of source code.

Filter by
Sorted by
Tagged with
8 votes
13 answers

It's Just Rocket Science

Write a program/function that finds the amount of fuel needed to escape Earth's gravity well given the exhaust velocity of the fuel and the amount of mass to transport using the Tsiolkovsky rocket ...
20 votes
27 answers

Rotational Average

Given an input integer n >= 10, output the average of all deduplicated rotations of the integer. For example, for input 123, ...
12 votes
7 answers

Create a nibble shorthand

I recently stumbled across this image on wikimedia commons. It's a little bit of an information overload at first, but after examining it a bit it shows an interesting number system for writing ...
20 votes
27 answers

Anti-divisors of a number

Given a positive integer n, output all of its anti-divisors in any order. From OEIS A006272: Anti-divisors are the numbers that do not divide a number by the ...
140 votes
556 answers

Shortest infinite loop producing no output

Your task is to create the shortest infinite loop! The point of this challenge is to create an infinite loop producing no output, unlike its possible duplicate. The reason to this is because the code ...
47 votes
115 answers

Halloween Golf: The 2spooky4me Challenge!

A current internet meme is to type 2spooky4me, with a second person typing 3spooky5me, following the (n)spooky(n+2)me pattern. Your mission is to implement this ...
16 votes
11 answers

Carryless factors

Carryless multiplication is an operation similar to binary long multiplication, but with XOR instead of addition: ...
11 votes
18 answers

Count the number of compositions of \$n\$ in which the greatest part is odd

A composition of an integer \$n\$ is a representation of \$n\$ as a sum of positive integers. For example the eight compositions of 4 are as follows: ...
17 votes
34 answers

Mathematical Combination

Write a program that takes an input such as: n,k which then computes: $$\binom n k = \frac {n!} {k!(n-k)!}$$ and then prints the result. A numerical example: ...
13 votes
19 answers

Restricted Integer Partitions

\$P_k(n)\$ means the number of partitions of \$n\$ into exactly \$k\$ positive parts. Given \$n\$ and \$k\$, calculate \$P_k(n)\$. Tip: \$P_k(n) = P_k(n−k) + P_{k−1}(n−1)\$, with initial values \$P_0(...
25 votes
62 answers

Find the sum of the divisors of N

Write a program that displays on the screen the sum of the divisors of a number (1 ≤ N ≤ 100) entered by the user in the range of 1 to N. This is OEIS A000203. Examples: Input: 7 ...
25 votes
46 answers

Product of Divisors

Challenge Given a positive integer, return the product of its divisors, including itself. This is sequence A007955 in the OEIS. Test Cases 1: 1 2: 2 3: 3 4: 8 5: 5 6: 36 7: 7 8: 64 9: 27 10: 100 ...
20 votes
2 answers

Can you connect the dots?

This challenge is based off of Flow Free. An online version can be found here: You will be given a puzzle, and you must return 1 if the ...
28 votes
27 answers

Antisymmetry of a Matrix

A matrix is antisymmetric, or skew-symmetric, if its transpose equals its negative. The transpose of a matrix can be obtained by reflecting its elements across the main diagonal. Examples of ...
20 votes
16 answers

Calculate Pi unto a point using the Nilakantha series

Your task: given a nonzero positive number i, calculate pi using the Nilakantha series unto i terms. The Nilakantha series is as ...
15 votes
28 answers

Calculating Pi using the Gregory Leibniz series until n terms

based off my previous challenge, this wikipedia article, and a Scratch project Your task: given i, calculate \$\pi\$ till i ...
11 votes
4 answers

Compute the Fabius Function

The Fabius function is an example of a function that is infinitely differentiable everywhere, yet nowhere analytic. One way to define the function is in terms of an infinite number of random variables....
-5 votes
6 answers

I'm Blue, Da ba dee da ba die

Write a program, which outputs I'm Blue, and then Da, Ba, Dee, Da, Ba, Die, forever, the words must be space-seperated in one ...
25 votes
14 answers

Print every Fool's Mate

In chess, the Fool's Mate is the fastest possible way to reach checkmate. It is reached with Black's second move. In chess notation, one possible sequence of moves that achieves Fool's Mate is ...
16 votes
7 answers

Translate Text into Matoran

The Matoran Alphabet is the alphabet used by many of the characters in the Bionicle universe. Your challenge is to create a program or function that takes a string as input and creates an image of ...
95 votes
123 answers

Shortest code to produce non-deterministic output

You need to produce output that is non-deterministic. In this case, this will be defined to mean that the output will not always be the same result. Rules: A pseudo-random number generator that ...
24 votes
10 answers

Name the poker hand

Name the poker hand Given five cards, output the name of the poker hand, which will be one of: ...
103 votes
315 answers

Simple cat program

One of the most common standard tasks (especially when showcasing esoteric programming languages) is to implement a "cat program": read all of STDIN and print it to STDOUT. While this is named after ...
9 votes
7 answers

Produce n n-squares of integers with rotating *'s in consecuitive corners

The objective is to produce output of n squares (nxn) of random integers (0-9) with a moving * that rotates clockwise around the ...
37 votes
56 answers

Am I not good enough for you?

Background: The current Perfect Numbers challenge is rather flawed and complicated, since it asks you to output in a complex format involving the factors of the number. This is a purely decision-...
58 votes
56 answers

Print every printable ASCII character without using it

In a programming language of your choice, write 95 programs, each of which outputs a different one of the 95 printable ASCII characters without that character occurring anywhere in the program. For ...
15 votes
16 answers

Computing a specific coefficient in a product of polynomials

Generator functions This gives the context for why this challenge came to life. Feel free to ignore. Generator functions are a nice way of encoding the solution to a problem of combinatorics. You ...
22 votes
55 answers

Minimum excluded number

This is intended to be an easy, bite-size code-golf. The mex (minimal excluded number) of a finite collection of numbers is the smallest non-negative integer ...
22 votes
23 answers

Iteratively delete a list

Say I have a ragged list, like: [ [1, 2], [3, [4, 5] ] ] And I want to iterate over each item and delete it one-by-one. However, I don't know the ...
45 votes
31 answers

XOR multiplication

You goal is to implement the operation of XOR (carryless) multiplication, defined below, in as few bytes as possible. If we think of bitwise XOR (^) as binary ...
52 votes
49 answers

Outputting ordinal numbers (1st, 2nd, 3rd)

I would like to generate (as a return result of a function, or simply as the output of a program) the ordinal suffix of a positive integer concatenated to the number. Samples: ...
16 votes
8 answers

Scribble Pad for Nerds

aka. implement an easier version of 05ab1e's canvas element. Description The canvas element is used to draw ASCII lines on the screen. The (easier version of the) canvas element takes a list of ...
42 votes
21 answers

Print the Periodic Table

Your challenge is to print/output/return this text: ...
8 votes
9 answers

Selective Caesar Cipher

Input A string of text. Output The string of text, however, every consonant is the next consonant in the alphabet, and every vowel is the next vowel in the alphabet (z becomes b, u becomes a). The ...
505 votes
932 answers

"Hello, World!"

So... uh... this is a bit embarrassing. But we don't have a plain "Hello, World!" challenge yet (despite having 35 variants tagged with hello-world, and counting). While this is not the most ...
-9 votes
7 answers

Write "tut-tut-tut" without using "tut"

Following this question: Tut-tut-tut-tut-tut Write a program that writes "tut-tut-tut" without using the characters "t", "u" or "-" in the code itself. This is ...
78 votes
62 answers

Draw the Ukrainian Flag

As you will probably know, there is a war going on in Ukraine. I noticed that it seems nobody has posted a Ukrainian flag challenge yet, so I thought I'd do it myself in support of Ukraine. The ...
26 votes
12 answers

Was it a King or a Queen or neither?

Input A calendar year from 927 to 2022. Output You can use any three distinguishable outputs. As an example, “K” (short for King) or “Q” (short for Queen) depending on which of the two England had in ...
14 votes
21 answers

N-dimensional pyramid numbers [duplicate]

Given two inputs, a number n and a dimension d, generate the nth d-dimensional pyramid number. That was confusing, let me try again. For d = 1, the numbers start 1,2,3,4,5 and is the number of points ...
39 votes
22 answers

Tips for golfing in Rust

While Rust is very, very rarely competitive in code golfing competitions (Java is often shorter), it can still be fun to golf in. What are some tricks for making Rust code shorter? Please post only ...
42 votes
45 answers

Print all 2 letter Scrabble Words

The Challenge: Print every 2 letter word acceptable in Scrabble using as few bytes as possible. I have created a text file list here. See also below. There are 101 words. No word starts with C or V. ...
25 votes
31 answers

Generate the Chess Numbers™

I was browsing Stack Overflow when I saw this post in Puzzling in the hot network questions and decided to make a challenge out of it (all credit goes to the creator of the challenge). Overview A ...
19 votes
12 answers

Random Golf of the Day #3: Integer Partitions

About the Series First off, you may treat this like any other code golf challenge, and answer it without worrying about the series at all. However, there is a leaderboard across all challenges. You ...
56 votes
68 answers

Am I a Fibonacci Number?

Your Task: Write a program or function to check if a number that is inputted is a Fibonacci number. A Fibonacci number is a number contained in the Fibonacci sequence. The Fibonacci Sequence is ...
34 votes
42 answers

Previous Fibonacci number

The sequence of Fibonacci numbers is defined as follows: \$ F_0 = 0 \\ F_1 = 1 \\ F_n = F_{n-1} + F_{n-2} \$ Given a Fibonacci number, return the previous Fibonacci number in the sequence. You do not ...
21 votes
15 answers

Polyglot Quiz (Cops' thread)

This is the cops' thread of a cops-and-robbers challenge. The robbers' thread can be found here In this challenge as a cop you will choose two programming languages A and B, as well as a non-empty ...
33 votes
17 answers

An Ant on a Cube

An ant walks along the edges (not faces) of a wireframe cube. Each vertex it encounters presents it with a fork from which two new edges branch off. The ant chooses which way to turn -- ...
21 votes
20 answers

Sample integers that sum to one hundred

Given a positive integer n, randomly output n non negative integers that sum to one hundred. n will be at most 200. The output should be present as a list of integers (not sorted). Your random sample ...
36 votes
58 answers

Compute the Median

Challenge Given a nonempty list of real numbers, compute its median. Definitions The median is computed as follows: First sort the list, if the number of entries is odd, the median is the value in ...
13 votes
21 answers

The Double's Base

Background IEEE 754 Double-precision floating-point format is a way to represent real numbers with 64 bits. It looks like the following: A real number n is ...

2 3 4 5