r/codegolf • u/Slackluster • Dec 26 '25
r/codegolf • u/Aspie_Astrologer • Jul 17 '23
(Meme) "I'm something of a golfer myself..."
r/codegolf • u/BetaKors_ • Oct 12 '22
[Python] Prime number checker (97 bytes)
import decimal as d
f=lambda n:n*f(n-1)if n>1else d.Decimal(1)
p=lambda n:n>1and(f(n-1)+1)/n%1==0p takes a Decimal as an argument and returns a bool telling whether or not the input is a prime number. f calculates the factorial of a Decimal. I tried implementing this way of calculating the factorial of a number for Decimals, but I kept getting a InvalidOperation error, so for now this is the smallest code I could manage.
If you used floats for this instead of Decimals, you would notice that the function would start spitting out wrong answers very quickly because of floating-point errors. So yeah, that's why I used Decimals.
Here's some code-golfed code (275 bytes) that prints out all prime numbers from 0 to n:
import sys,decimal as d
n=100
f=lambda n:n*f(n-1)if n>1else d.Decimal(1)
p=lambda n:n>1and(f(n-1)+1)/n%1==0
sys.setrecursionlimit(n+3)
d.getcontext().prec=len(v)if'E+'not in(v:=str(f(d.Decimal(n))))else int(v[v.find('E+')+2:])
print(', '.join(str(i)for i in range(n)if p(i)))Note that the code also changes the recursion limit, because there would be a RecursionError for values above 997. It also changes the precision of Decimals, since a DivisionImpossible exception would be thrown for values whose factorial has 28 (the default precision of Decimals) or more decimal places. As such, the code does some stuff with strings to find out what the precision should be to accommodate for the maximum value whose factorial we will have to calculate — and then use in a division —, in this case being n.
For some reason, on my machine, the code stops working for values above 1994, with no errors being shown.
Also, if you don't change the precision of Decimals and call p with 28 as the argument, it'll return True, even though 28 is obviously not a prime number. It also won't throw an exception, even though the factorial of 28 has more than 28 decimal places (with 29 as the argument, however, it'll actually throw the exception). No clue why.
r/codegolf • u/BetaKors_ • Oct 11 '22
[Python] My take on the dogAgeYears thingy (82 bytes)
Here's my take on trying to make the code from this post as small as possible!
c=lambda n:['0 - 15',15,24,28][n]if n<5else n*5+12if n<15else"That's a long life!"This code doesn't exactly match the original implementation because negative numbers are indexed into that list, and since Python supports negative indexing, the function simply returns the value that was indexed. Unless of course, that negative number is smaller than -4, at which case it throws an IndexError.
What should happen (based only off of the code in that post, so assuming that negative numbers are a possibility) is that the default case should run, so the function should return "That's a long life!".
btw i originally saw that post in this crosspost
r/codegolf • u/matigekunst • Oct 03 '22
Code golfing American Psycho's new business card
r/codegolf • u/matigekunst • Jul 28 '22
Patrick Bateman's fractal flame business card now in 1337 bytes
#include <stdlib.h>
#include <math.h>//gcc -o c c.c -lm;./c>c.ppm
typedef float f;typedef int d;d R=600,C=1050;
f z(f x,f o){return fmod(fmod(x,o)+o,o);}f o=
1/22.,p=.04,H=.5,V=.25,Z=1.5,e=.07,b=.14;main
(){printf("P6 %i 600 255 ",C);f (*q)[C];q=
malloc(4*R*C);f I[25][6]={{0,-b,b,0,-3,1},{
0,-b,p,0,-2,Z},{e,0,0,b,-3,2},{e,0,0,b,-3,1},
{p,-b,b,p,-1,1},{p,b,-b,p,-H,Z},{p,0,0,b,-1,1
},{b,0,0,b,1,2},{0,-b,b,0,Z,1},{0,-b,b,0,3,1,
o},{0,-b,p,0,4,Z},{e,0,0,b,3,2},{e,0,0,b,3,1}
,{b,p,-p,b,3,1},{0,-b,b,0,5,1},{e,0,0,b,7,2},
{e,0,0,b,7,H},{0,-b,b,0,7,1},{0,-b,b,0,9,1},{
e,-p,e,b,9,Z},{e,b,-e,b,9,1}},J[25][6]={{0,-b
,b,0,-3,1},{e,0,0,b,-3,1},{e,0,0,b,-3,V},{0,-
b,p,0,-2,H},{p,-b,b,p,-1,1},{p,b,-b,p,-H,Z},{
p,0,0,b,-1,1},{b,0,0,b,1,2},{0,-b,b,0,Z,1},{e
,0,0,b,3,2},{e,0,0,b,3,H},{0,-b,b,0,3,1},{p,0
,0,b,3,1},{0,-b,b,0,5,1},{0,-b,b,0,6.5,1},{p,
-b,p,p,6,Z},{p,b,-b,p,5,2},{p,-b,b,p,7,1},{p,
b,-b,p,7.5,Z},{p,0,0,b,7,1},{0,-b,b,0,9,1},{0
,-b,b,0,9.9,1},{p,b,-b,p,9,Z}};d S=22;f t=H,s
=H,Y=H,X=-b,L=R*C;for(d i=2*L;i--;){if(i==L){
o=p;memcpy(&I,&J,R);S=25;Y=0;}f a=(f)rand()/
RAND_MAX;f P=0;for(d z=S;z--;){P+=o;if(a<P){f
G=t*I[z][0]+s*I[z][1]+I[z][4];s=t*I[z][2]+s*
I[z][3]+I[z][5];t=G;break;}}d W=(d)(z((s*b+Y)
,1)*R);d B=(d)(z(1.7-t*e,1)*C);q[W][B]=1;}for
(d x=R;x--;){for(d y=C;y--;){d E=(d)((1-q[x][
y])*255);printf("%c%c%c",E,E,E);}}}r/codegolf • u/rschwa6308 • Feb 16 '22
A List of Techniques for Golfing in Python
russ-stuff.comr/codegolf • u/Mental-Spinach-3149 • Sep 24 '21
Codegolf but for run time rather than code length
Can anyone point me to where to find this? i.e. code golf with more of a practical bent?
For example, I'm thinking of code which would naively be done with for loops but can be run orders of magnitude faster with array operations
r/codegolf • u/JeromePaulos • Aug 10 '20
An 81 byte color shade generator
The Challenge
I'm making an SVG heatmap where each path needs to be colored a different shade based on its percentage of the largest value. Given two colors and a percentage, generate an RGB color shade between those colors based on the percentage. The percentage can have any number of decimal places, however it will not be more than 1 or less than 0.
Example
getColor(0.5, [0,0,0], [255,255,255]); // Outputs rgb(128,128,128)My Solution
My solution is in JavaScript, which is most useful to me, but it would be interesting to see other languages. Is it possible to get it any smaller?
function getColor(n,o,r){return"rgb("+r.map((r,t)=>r+n*(o[t]-r)|0).join(",")+")"}Graphic

r/codegolf • u/Hell__Mood • Sep 15 '19
3D animation with sound in 64 bytes of assembler
r/codegolf • u/mathishammel • Dec 15 '18
Advent of Code Golf
I am running a golfing contest on r/AdventOfCode, thought some of you might be interested :)
The contest will be based on problem 20 of Advent of Code, which will be published next Thursday.
Rules :
There is nothing to win but more importantly nothing to lose, beginners are more than encouraged to play!
-The contest starts when problem 20 is published, and submissions are open for 4 days (until Monday's problem is published).
The program has to print exactly two lines : first line is the output for part one, second line is the output for part two of the problem. Both lines need to match the formatting and correct result for the problem.
The input can be read from stdin or from a file, or hardcoded (in which case it will count towards the byte count of your solution).
The program has to work on more than your input. In case of doubt, I will test the execution against my account's input.
Submit sources as an email attachment to [email protected], preferably in a single text file. The score of the submission is the size (in bytes) of the attachment.
You are allowed to submit multiple solutions. If there is enough diversity in the style/language of your solutions, I might feature several of them in the results.
If you submit a solution in a golf-specific language, please submit another one in a non-esoteric language. Golf-specific languages will be kept in a separate scoreboard.
There will be a global scoreboard, but the best submission(s) in each language will be highlighted as well.
Be fair - don't try to find unreasonable loopholes
Be creative!
r/codegolf • u/alokmenghrajani • Aug 04 '18
Bootable CD + retro game in a tweet
r/codegolf • u/wtf_are_my_initials • Feb 02 '16
[JS] Chat application in 140 bytes
r/codegolf • u/Aspie_Astrologer • Feb 21 '21
Code Golf: Battle Royale Thread (Challenges in Comments)
Who feels like some Code Golf Battles?
Every comment to this post can start with **Challenge**: and define a (relatively straight-forward) input-output problem to solve in the least number of characters in any language.
Responses go in sub-comments and can start with e.g. **Python (74):** to indicate the language and character length of the solution.
Use backticks `` to enclose code (edit: or use four spaces as an indent to do multiline code).
e.g. Challenge: Input positive integer, print the sum of all primes up to and including that integer - but with every second prime added as a negative.
e.g. Ruby (68):
require'prime'
i=-1
p Prime.each(gets.to_i).map{i+=1
_1*(-1)**i}.sumr/codegolf • u/s3ddd • Dec 09 '13
Caesar Cypher
Caesar cipher - Implement a Caesar cipher, both encoding and decoding. The key is an integer from 1 to 25. This cipher rotates the letters of the alphabet (A to Z). The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A). So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to "BC". This simple "monoalphabetic substitution cipher" provides almost no security, because an attacker who has the encoded message can either use frequency analysis to guess the key, or just try all 25 keys.
Here is my solution (as a bash script):
#!/bin/bash
# Sample usage:
# ./bash_caesar <key (1-25)> <input file>
tr 'A-Z' 'a-z' < $2 | tr 'a-z' $( echo {a..z} | sed -r 's/ //g' | sed -r "s/(.{$1})(.*)/\2\1/" )Originally I posted this here
r/codegolf • u/dantose • Dec 01 '25
Advent of Code, Day 1
Post your best golfs.
Assume input is saved as input.txt.
r/codegolf • u/cthmsst • Jan 04 '24
Jugly.io: a minimalist JavaScript code golfing plateform
Hello fellow golfers!
I've recently made Jugly, a free web app designed for JavaScript code golfing. My goal was to create a place where simplicity meets challenge, allowing to focus on what we love most: crafting the shortest, least elegant code possible.
Pure JavaScript Challenges
Integrated monaco editor
Leaderboards
I built Jugly because I love code golfing in JS and thought it'd be cool to have a spot where we can all share that. It's a laid-back place for anyone who wants to play around with code, learn a bit, and maybe show off some.
Fancy a round of golf? Swing by https://jugly.io and see how few characters you can get away with!
It's a little project I whipped up in my spare time, nothing too fancy. I'm really looking forward to hear your feedback and see your names shining on the Jugly leaderboards!
r/codegolf • u/superogue • Feb 05 '23
Lovebyte 2023 - Only 5 days left until the biggest online codegolf/sizecoding party is about to start!
Lovebyte 2023 : 10-12 February 2023 ( https://lovebyte.party )
Join us in a celebration of the smallest with a dedicated sizecoding demoparty, held on the weekend of 10-12th February 2023 on Discord and Twitch ( https://www.twitch.tv/lovebytedemoparty )
This year we will take it to the next level with intro competitions in different size categories from 16 bytes to 1024 bytes. From our Tiny Executable Graphics and Nanogame competitions to Tiny CGA Pixel Graphics and Bytebeat Music competitions.
Or what about cool size-coded related seminars to get you started? Or otherwise our Bytejam, Introshows, DJ Sets and the many other events we have lined up for you. We welcome everyone from newcomers to veterans and are open to all platforms. From Pldschool 6502 and Z80 platforms like the Atari, Commodore, Amstrad & ZX Spectrum to High-end X86/ARM/RISC platforms and Fantasy Console platforms.
And for those that would like to join the fun and get creative: We have our party system ready to receive your entries at https://wuhu.lovebyte.party/. Contact us via the Lovebyte discord or socials to request your vote/registration key.
This is the one event where size does matter! Don't miss it!
Website: https://lovebyte.party/
Twitch: https://www.twitch.tv/lovebytedemoparty
Youtube: https://www.youtube.com/@Lovebytedemoparty
Discord: https://discord.gg/pUS5kCJTzp
Mastodon: https://graphics.social/@lovebyteparty
Twitter: https://twitter.com/lovebyteparty
Instagram: https://www.instagram.com/lovebyteparty
r/codegolf • u/Hell__Mood • Oct 04 '20
Gespensterwald - 3D animation with ambient drone in 64 bytes of x86 code
r/codegolf • u/Hell__Mood • Sep 13 '20