CodeWalrus Website > Contests

Code Golf Contest: The Pythagorean Problem

(1/5) > >>

Juju:
Time for a code golf contest? Been a while we did that. Thought that'd be fun while I'm busy with other stuff. If you don't remember what it is, I give you a task and you try to write a program that is the shortest you can do. Standard rules apply, you can find them on Stack Overflow. If in doubt, like how to count the size of your entry, just ask me or anyone familiar with the previous contests we did before.

So yeah, this one revolves around Pythagorean triplets. If you don't know what it is either, it's any set of integers (a,b,c) that solves the formula a2 + b2 = c2. So if you take a point on a plane, say (3,4), it should be at a distance 5 from the origin (0,0). Same for (4,3). The point (3,5), though, is at a distance √34 from the origin, which is not an integer and therefore not Pythagorean.

So your task is, you are given a monochrome graphic screen (96x64 on a TI-83+, but could be anything big enough we can see something) with the origin (0,0) in a corner (usually upper left, but could be lower left) and you must turn on every pixel (a,b) that verifies a2 + b2 = c2 with a, b and c being integers, with the rest must be off. Don't miss one! I know there's a bunch of clever tricks to do that, so be creative.

Bonus task: Write another program that counts how many pixels are on this way for any width and height given as inputs.

Send the source code either in this thread on in PM to me before September 1st, 23:59:59 EST. and make it clear whether the codes you send are for the main task or the bonus one. There's nothing at stake other than the honor, feel free to help each other and have fun!

Standings

UserLanguageBytes@JujuRuby52
* Entries must be valid and verified before showing up in this table.

c4ooo:
I don't understand, if (a,b) is the pixel coords what is c?
Am I supposed to check if sqrt(a*a+b*b) is an integer?

Juju:
Exactly. c a bit irrelevant as you can derive it from a and b, but yeah, pretty much.

kotu:
Can I use C++

jamu:
so this works in python?

--- Code: ---import math
points = [(x,y) for x in range(1, 96) for y in range(1, 64) if math.sqrt(x * x + y * y).is_integer()]
--- End code ---

producing this when scatter plotted: