Site: code.golf
Write shortest code to do something. Existing problems broken up into categories.
Site: code.golf
Write shortest code to do something. Existing problems broken up into categories.
Some problems have a long version, usually meaning the shorter version has the possibility of having a better solution just printing the numbers instead of calculating them.
Abundant Numbers - sum of proper divisors is greater than itself
Emirp Numbers - find number that backwards and forwards is prime and different from each other
Evil Numbers - number has even number of 1's in binary
Happy Numbers - continued summing of squares of digits will end at 1
Niven Numbers - number divisible by sum of digits
Odious Numbers - number has odd number of 1's in binary
Pernicious Numbers - number had prime numbers of 1's in binary
Smith Numbers - number is divisible by sum of digits of prime factors
Vampire Numbers - number can be factored into two numbers such that factors have half as many digits as the number
Collatz - find number of steps to 1
Divisors - find all divisors of a number
Leap Years - print leap years from 1800 to 2400
Leyland Numbers - up to 100,000,000,000
Next output has dependency on previous output.
Catalan Numbers - last number is 57 digits long
Given primarily numerical inputs, perform the given computation
Reverse Polish Notation - evaluate RPN expression
Fractions - reduce a given fraction
Intersection - compute the area of intersection of two boxes on a plane
Lucky Tickets - given number of digits and a base, compute number of tickets that meet a condition
Repeating Decimals - given a fraction, print the decimal expansion, notating repeating digits
ISBN - calculate checksum of ISBN number
Time Distance - round seconds to closest larger unit
Zodiac Signs - Date/time to zodiac sign
Compute something on a string of text
CSS Colors - Map colors names to hex values
Emojify - convert emoticon to emoji
Hexdump - mimic output of hexdump on string
Levenshtein Distance - calculate number of edits between two strings
United States - Map state names to state abbreviations
Print lines of texts. Output is usually fixed. Usually the text has more computability than entries in string compression.
ASCII Table - mimic output of 'man 7 ascii'
Christmas Trees - trees made of asterisks (*)
Cubes - wireframe cubes
Diamonds - diamonds made of numbers
Number Sprial - 10x10 grid from 0 to 99 in middle
Sierpiński Triangle - printed with (▲)
Seven Segment - input varies
Star Wars Opening Crawl - input varies
Input is 2D data in ASCII
Proximity Grid - reachability analysis of given grids
Game of Life - apply one step of Game of Life
Maze - solve a given maze
Print lines of text, usually as part of a song. No input, output always the same.
Tongue Twisters - woodchuck, Peter Piper, and Sally's seashells
Print numeric expansion of some constant. Tests compression of data consisting of only 0-9, 1000 digits.
Derek increases his lead by submitting a new leading solution to Collatz.
Derek attempts a challenge to Factorial Factorization, but falls a little short.
Derek improves Factorial Factorization closing the gap by 14.
Richard T improved his Collatz solution, reducing Derek's lead to just 1.
Derek's hint of sscanf allows Richard to improve his Repeating Decimals solution by 7, but still trails by 4.
Richard T eliminates a loop and moves ahead by 8 on Divisors, taking the lead for the first time.
Derek improves on Factorial Factorization, but still trails by 9.
Derek finds a different way in Divisors and ties up everything.
Derek and Richard T tie on Fractions.
Derek trails in ISBN by 15
Derek increases the lead on Repeating Decimals to 10
Derek is uncontested in United States
Derek is uncontested in Zeckendorf Representation
Richard T takes back the lead by Zeckendorf Representation by 2.
Derek closes in on ISBN, only 2 away.
Derek opens a new front by announcing an attack on Lucky Tickets. Richard T takes this opportunity to improve defense by 3 points, but Derek succeeds in blowing by with a 9 point lead.
Derek takes the largest lead so far getting a 46 point lead in Lucky Tickets.
Derek modulates his way to tying on ISBN.
Derek lands a heavy strike on Sierpinski Triangle with a 46 point lead.
Richard T retaliates with a 13 point lead on Sierpinski Triangle
Richard T makes a minor improvement to Sierpinski Triangle, still behind by 44.
Derek opens up more questions by taking on ASCII Table, ahead by 10 and Odious Numbers, trailing by 1.
Derek improves Sierpinski Triangle by 5
A new player has joined!!!!
Catch up on records.
Derek opens up several new fronts in the battle and takes a commanding lead.
Richard T uses a shorter type name to break the tie on Transpose Sentence.
Derek
Abundant Numbers (90)
Abundant Numbers (long) (91)
Arithmetic Numbers (104,+4)
Ascending Primes (126,+2)
ASCII Table (142)
Card Number Validation (121)
Collatz (92)
Divisors (82)
Emirp Numbers (120)
Emirp Numbers (Long) (120)
Evil Numbers (84)
Evil Numbers (long) (82,+1)
Factorial Factorization (129,+9)
Farley Sequence (118,+9)
Fibonacci (69)
Fizz Buzz (99,+1)
Foo Fizz Buzz Bar (130)
Factions (132,+6)
Gijswijt's Sequence (153)
Happy Numbers (long) (116)
ISBN (122)
Leap Years (76)
Leyland Numbers (166,+1)
Look and Say ()
Lucky Numbers (163,+33)
Lucky Tickets (178)
Odious Numbers (83)
Odious Numbers (long) (84)
Ordinal Numbers (117)
Palindromemordnilap (150,+16)
Pernicious Numbers (84)
Pernicious Numbers (long) (86)
Prime Numbers (77)
Prime Numbers (long) (78)
Quine (103,+20)
Repeating Decimals (217)
Sierpiński Triangle (92)
Smith Numbers (149)
Transpose Sentence (149,+1)
Zeckendorf Representation (126,+2)
Richard
Abundant Numbers (326,+236)
Abundant Numbers (long) ()
Arithmetic Numbers (100)
Ascending Primes ()
ASCII Table (152,+10)
Card Number Validation ()
Collatz (94,+2)
Divisors ()
Emirp Numbers ()
Emirp Numbers (Long) ()
Evil Numbers ()
Evil Numbers (long) ()
Factorial Factorization (122,+2)
Farley Sequence ()
Fibonacci (71,+2)
Fizz Buzz (99,+1)
Foo Fizz Buzz Bar ()
Factions ()
Gijswijt's Sequence ()
Happy Numbers (long) ()
ISBN (122)
Leap Years ()
Leyland Numbers ()
Look and Say (111)
Lucky Numbers ()
Lucky Tickets ()
Odious Numbers ()
Odious Numbers (long) ()
Ordinal Numbers ()
Palindromemordnilap ()
Pernicious Numbers ()
Pernicious Numbers (long) ()
Prime Numbers (81,+4)
Prime Numbers (long) (81,+3)
Quine (83)
Repeating Decimals ()
Sierpiński Triangle (92)
Smith Numbers ()
Transpose Sentence ()
Zeckendorf Representation ()
Richard T
Abundant Numbers (93,+3)
Abundant Numbers (long) (inf)
Arithmetic Numbers (104,+4)
Ascending Primes (124)
ASCII Table (152,+10)
Card Number Validation (128,+7)
Collatz (93,+1)
Divisors (82)
Emirp Numbers (157,+37)
Emirp Numbers (Long) (152,+32)
Evil Numbers (84)
Evil Numbers (long) (81)
Factorial Factorization (120)
Farley Sequence (109)
Fibonacci (70,+1)
Fizz Buzz (98)
Foo Fizz Buzz Bar (138,+8)
Factions (126)
Gijswijt's Sequence (175,+22)
Happy Numbers (long) (118,+2)
ISBN (122)
Leap Years (77,+1)
Leyland Numbers (155)
Look and Say (132,+21)
Lucky Numbers (130)
Lucky TIckets (221,+43)
Odious Numbers (83)
Odious Numbers (long) (84)
Ordinal Numbers (128,+11)
Palindromemordnilap (134)
Pernicious Numbers (84)
Pernicious Numbers (long) (86)
Prime Numbers (80,+3)
Prime Numbers (long) (81,+3)
Quine ()
Repeating Decimals (226,+9)
Sierpiński Triangle (92)
Smith Numbers (154,+5)
Transpose Sentence (148)
Zeckendorf Representation (124)
Every trick I know for golfing
code.golf specific tricks
#import<bits/stdc++.h> will make most of the standand libaries available
#import<ios> gets most C I/O functions, such as printf, sscanf, and atoi
__builtin_printf is available without any headers
Use floating point constants to replace larger numbers, 1e4 instead of 1000
Logical-and can sometimes be replaced by bitwise-and or multiplcation and logical-or and be replaced by bitwise-or or addition in some circumstances.
The ternary operator (?:) and short-circuit of the logical-and (&&) and logical-or (||) can be used in control flow.
cond && printf("only print on true cond");
cond || printf("only print on false cond");
The ternary operator has an extension to allow an empty true statement
a?:c is the same as a?a:c
The comma operator can be used in the true branch to have multiple statements, but the false branch will need parens.
a?b1,b2:(c1,c2)
Use as few print statements as possible. If many are needed, capturing output stream or printf function pointer will save characters on subsequent calls to print statements.
auto&o = std::cout << "Hi";
auto*p = printf;
Tadpole operators (-~ and ~-) to get value +/-1 with higher precedence than adding and subtracting
Replace char literals with their ascii values. 65 instead of 'A'
Integer condition checks
a != b is the same as a-b in bool context
a/b is non-zero when a > b, good for use in arithmetic operations without needing parens
a!=0 can be changed to just a
~a will be 0 when a is -1, useful with sscanf return value
~-a will be 0 when a is 1
Looping checks
Putting increment/decrement in the loop condition to save a statement
Instead of looping 0 to N, go from N to 0
for (int i = 25; --i; );
printf returns the number or characters printed. If the last printed is the longest to be printed, use the return value in the loop check.
sscanf will return -1 on failure. Useful when working with multiple argv strings.
Crashing on a segmentation fault counts as a successful program end. One way to do this is by iterating over argv until it crashes on a null string. Calling main over and over with an incremented argv will traverse the array.
int main(int argc, char* argv[]) {
char *c = *++argv;
int x = atoi(c);
main(argc, argv);
}
Loops
for(;cond;) is the same length as while(cond), but for has extra spots to put statements to save a semi-colon.
If the loop body only has one statement, the parens can be removed
Combine loops if possible to save characters. Both nested and serial loops can be combined in various ways.
Prime numbers, divisors
Using modulus operator (%) to find if a number is a divisor
All number have two divisors, 1 and itself. Primes will only have those two.
Prime checks can happen either count up or count down. If the first divisor found is the number itself, or 1, then it is prime. Using a post decrement, the 1 can be made into a 0 for an easier check later.
To get prime factors of a number, the smallest divisor greater than 1 is a prime. Each prime factor must be divided from the number, possibly multiple times, before finding the next factor.