Questions tagged [code-golf]

Code-golf is a competition to solve a particular problem in the fewest bytes of source code. If you want to score by characters instead of bytes, please state this explicitly in the challenge. If source code length is not the primary scoring criterion, consider using another tag instead.

Filter by
Sorted by
Tagged with
68
votes
65answers
9k views

This is my pillow

...will you help me immortalize it? I've had this pillow a few years now, and apparently it's time to get rid of it. Can you please write a function or program, that I can bring with me and use to ...
52
votes
130answers
8k 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: ...
24
votes
9answers
2k views

Spell out numbers in French

There was a challenge to convert a number to its numeral in English, but that was too straightforward. Given a number 0–100, your task is to output the corresponding numeral in French. The French ...
19
votes
14answers
3k views

Four Squares Together

Lagrange's four square theorem tells us any natural number can be represented as the sum of four square numbers. Your task is to write a program that does this. Input: A natural number (below 1 ...
19
votes
9answers
5k views

Generating PBM bitmap files from ASCII text

The PBM (Portable BitMap) format is a very simple ASCII black and white bitmap format. Here's an example for the letter 'J' (copy-pasted from the wikipedia link): P1 # This is an example bitmap of ...
59
votes
54answers
3k views

Count trailing truths

Inspired by, and in memory of, my dear friend and colleague, Dan Baronet, 1956 – 2016. R.I.P. He found the shortest possible APL solution to this task: Task Given a Boolean list, count the number ...
51
votes
83answers
6k views

Find the smallest number that doesn't divide N

This challenge is simple enough that it's basically all in the title: you're given a positive integer N and you should return the smallest positive integer which is not a divisor of N. An example: ...
43
votes
10answers
4k views

Zigzagify a Matrix

As part of its compression algorithm, the JPEG standard unrolls a matrix into a vector along antidiagonals of alternating direction: Your task is to take a matrix (not necessarily square) and return ...
41
votes
45answers
3k views

Skip like a rabbit!

Given a list of non-negative integers in any reasonable format, iterate over it, skipping as many elements as every integer you step on says. Here is a worked example: ...
41
votes
16answers
3k views

String.prototype.isRepeated

UPDATE : isaacg's Pyth submission is the winner! Many of you must have heard that there is a cooler version of JavaScript in town (read ES6) which has a method ...
31
votes
30answers
5k views

Is it a Bumpy Word?

(inspired by this challenge over on Puzzling -- SPOILERS for that puzzle are below, so stop reading here if you want to solve that puzzle on your own!) If a letter in a word occurs alphabetically ...
30
votes
24answers
3k views

Palindromizing the strings

Introduction For the ones who don't know, a palindrome is when a string is equal to the string backwards (with exception to interpunction, spaces, etc.). An example of a palindrome is: ...
27
votes
33answers
2k views

Calculate the prime factors

We had a prime factorization challenge a while ago, but that challenge is nearly six years old and barely meets our current requirements, so I believe it's time for a new one. Challenge Write a ...
51
votes
43answers
5k views

Coprimes up to N

Given a number n >= 2, output all the positive integers less than n where gcd(n, k) == 1 (...
44
votes
132answers
6k views

Full Width Text

Full width text is text that has a space after every character, including the last one. For instance, the first sentence of this question becomes: ...
40
votes
20answers
2k views

Time-Sensitive Echo

Background The echo program is so neat. You can say anything to it, and it repeats your words perfectly, every time! How cool is that! Disappointingly, it repeats ...
31
votes
42answers
3k 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 ...
28
votes
47answers
3k views

Transpose a page of text

EDIT I modified the wording of the rules to make some things which were implicit more explicit. I also added some emphasis to clear up some points of apparent confusion, and explicitly defined the ...
27
votes
42answers
3k views

Sum of primes between given range

Write the shortest code for finding the sum of primes between a and b (inclusive). Input a ...
27
votes
35answers
4k views

Calculate Euler's totient function

Background Euler's totient function φ(n) is defined as the number of whole numbers less than or equal to n that are relatively ...
27
votes
12answers
4k views

Flipping pancakes

In pancake sorting the only allowed operation is to reverse the elements of some prefix of the sequence. Or, think of a stack of pancakes: We insert a spatula somewhere in the stack and flip all the ...
26
votes
2answers
1k views

Arranging arbitrary rectangles to fill a space

Can these rectangles fill a rectangular space? Given a bunch of rectangles, you are asked whether or not they can be arranged to fill a rectangular space. Specs Given a bunch of arbitrary ...
23
votes
26answers
3k views

There Was an Old Lady

Your goal is to write a program that prints the following poem exactly as it appears here: ...
19
votes
9answers
611 views

Given an input, move it along the keyboard by N characters

The Challenge: Given any input that can be typed on a keyboard, move the text along by N chars. Here is the QWERTY keyboard to be used. You can ignore the modifier keys (Shift, Caps, Enter, Delete ...
19
votes
20answers
1k views

Fluctuating ranges

Given a list with number, output the ranges like this: Input: [0, 5, 0] would become [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]. This ...
17
votes
2answers
986 views

Compile Regexes

In this task you have to write a program that reads a regular expression and generates another program that outputs whether an input string is accepted by that regular expression. The output must be a ...
12
votes
10answers
595 views

New Order #1: How does this feel?

Introduction Putting all positive numbers in its regular order (1, 2, 3, ...) is a bit boring, isn't it? So here is a series of challenges around permutations (reshuffelings) of all positive numbers. ...
48
votes
13answers
7k views

Extreme Fibonacci

There have been a billion iterations of Fibonacci challenges on this website, so lets spice things up with a Fibonacci challenge of a billion iterations! Your challenge is to output the first 1000 ...
32
votes
9answers
2k views

Rotate the anti-diagonals

Background In most reasonable programming languages, it's very easy to rotate the rows or columns of a 2D array. In this challenge, your task is to rotate the anti-diagonals instead. Recall that the ...
28
votes
7answers
3k views

Score a Cribbage Hand

This challenge is to score a Cribbage hand. If you don't play Cribbage, you've got some learning to do. We play with a standard poker deck, and a hand consists of four cards plus the "up card". ...
26
votes
20answers
4k views

Am I perfect (number)?

This is my first challenge! Background Perfect number is a positive integer, that is equal to the sum of all its divisors, except itself. So 6 is perfect number, ...
25
votes
11answers
3k views

Generate a pair of integers from a non-negative one

You should write a program or function which takes a non-negative integer N as input and outputs or returns two integers (negative, zero or positive) ...
24
votes
38answers
3k 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.
23
votes
27answers
4k views

Sierpinski Carpets

Who doesn't love a good fractal? The Sierpinski Carpet is a classic example of a fractal. To complete this task, you will be required to generate a carpet of type and print the resulting image to ...
23
votes
15answers
2k views

Coding Convention Conversion

In that Coding Golf, you should convert one coding convention with TitleCase to lower_case_with_underscores. And... vice versa! Specification Change the casing in a following way: If underscore ...
21
votes
10answers
1k views

Prime factors buddies

Given an integer N > 1, output all other numbers which prime decompositions have the same digits as the prime decomposition of ...
21
votes
17answers
1k views

Cambridge Transposition

I'm sure most, if not all, of you have come across this at some point or another: Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the ...
20
votes
1answer
866 views

Factor a polynomial over a finite field or the integers

Without using any built-in factoring/polynomial functions, factor a polynomial completely into irreducibles over the integers or a finite field. Input Your program/function will receive some prime (...
20
votes
22answers
1k views

Simplify matrix input!

I have written a few challenges related to matrices, and common for all are that I use a format like the one below when representing the matrices, both in examples and in test cases: ...
14
votes
35answers
2k views

Cartesian product of two lists

Task Given two lists of characters, output their Cartesian product, i.e. the list of pairings of each letter from the first list with each letter from the second list. Example ...
9
votes
5answers
719 views

Tips for golfing in Lisp

What general tips are these for golfing in Lisp (any dialect)? Please post one tip per answer, and only answers that are specific to a dialect of Lisp (e.g. "remove comments" is not an answer). ...
46
votes
44answers
4k views

Count sums of two squares

Given a non-negative number n, output the number of ways to express n as the sum of two squares of integers ...
38
votes
40answers
3k views

Is it a balanced number?

A number is balanced if the sum of the digits on each half of the number is equal, so: 1423 is balanced because 1+4 = 2+3, so is:...
29
votes
13answers
5k views

How many Jimmys can fit?

In this simple but fun challenge, you were asked to determine if Jimmy would fall of their platform. Jimmy has three body parts /, ...
28
votes
14answers
1k views

List all multiplicative partitions of n

Given a positive number n, output all distinct multiplicative partitions of n in any convenient format. A multiplicative partition of n is a set of integers, all greater than one, such that their ...
27
votes
22answers
1k views

Natural construction

The natural numbers including 0 are formally defined as sets, in the following way: Number 0 is defined as the empty set, {} For n ≥ 0, number n+1 is defined as n ∪ {n}. As a consequence, n = {0, 1, ...
19
votes
2answers
812 views

Dames, do some math!

The order of operations, PEMDAS, is a basic rule in mathematics telling us which order operations should be performed: "Parentheses, Exponents, Multiplication and Division, and Addition and ...
17
votes
3answers
966 views

Write a third order quine

This challenge is an extension of 'Golf a mutual quine'. Using three languages of your choice, create a third order Ouroboros program. That is, in language A, write a program pA which outputs ...
16
votes
6answers
897 views

Code Golf: Mix the nuts so that none of the same kind are touching

Input: Input is a randomized array of nuts (in your language), the possible nuts follow. Your program must have a way of representing each kind of nut, such as an integer code. Program must be able ...
14
votes
11answers
1k views

An Old Irish Blessing

I don't know who coined these words, and I'm not Irish, but I give you an Irish blessing: ...

1 3 4 5 6 7 63