September 21, 2019, 10:20:41 pm

## Code Golf Contest: The Pythagorean Problem

Started by Juju, August 15, 2017, 07:52:01 pm

0 Members and 2 Guests are viewing this topic.

#### Juju

##### August 15, 2017, 07:52:01 pmLast Edit: August 17, 2017, 05:02:31 pm by 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

 User Language Bytes @Snektron ShaderToy 50 @Juju Ruby 60

 User Language Bytes @Juju Ruby 52

* Entries must be valid and verified before showing up in this table.
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader --Snektron

if you wanna throw money at me and/or CodeWalrus monthly it's here

#### c4ooo

##### August 15, 2017, 08:27:04 pm #1 Last Edit: August 15, 2017, 08:31:44 pm by 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

##### August 15, 2017, 11:01:53 pm #2 Last Edit: August 16, 2017, 12:25:32 am by Juju
Exactly. c a bit irrelevant as you can derive it from a and b, but yeah, pretty much.
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader --Snektron

if you wanna throw money at me and/or CodeWalrus monthly it's here

#### kotu

##### August 15, 2017, 11:06:12 pm #3
Can I use C++
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### jamu

##### August 15, 2017, 11:08:33 pm #4
so this works in python?
`import mathpoints = [(x,y) for x in range(1, 96) for y in range(1, 64) if math.sqrt(x * x + y * y).is_integer()]`

producing this when scatter plotted:
I have a rhythm game problem.

#### kotu

##### August 15, 2017, 11:09:49 pm #5 Last Edit: August 15, 2017, 11:12:46 pm by kotu
Winner should be determined by processing time

*edit*
think i can do it about 4 or 5 characters shorter in c++builder (as the code that is added to a blank project with one control)
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### Juju

##### August 16, 2017, 01:29:25 am #6
Ideally, your program should be a complete, valid program you run (in cmd, bash or double-clicking it) that output a 96x64 image (or in the size you give as input) either on screen or in a file. So the scatter plot part should also be part of the program. Additionally, you'll want to save as much bytes as possible. You can use any programming language, but ideally it should fit in one file I compile/interpret and run. And of course, it shouldn't take 3 hours to run.
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader --Snektron

if you wanna throw money at me and/or CodeWalrus monthly it's here

#### kotu

##### August 16, 2017, 02:21:50 am #7
@Juju do you have Turbo C++ 2006? I might be able to make something pretty... meh
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### Juju

##### August 16, 2017, 03:32:48 am #8
Ideally, if you make it work with gcc, that'd be perfect. Also, the code you generated with the builder (that isn't in a third-party library) might count against the byte total.
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader --Snektron

if you wanna throw money at me and/or CodeWalrus monthly it's here

#### kotu

##### August 16, 2017, 03:42:15 am #9 Last Edit: August 16, 2017, 03:52:17 am by kotu
I did it in * here is the code
`f(096){f(064)`

*C++Builder
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### Juju

##### August 16, 2017, 05:15:25 am #10 Last Edit: August 16, 2017, 07:45:43 am by Juju
Code (Ruby) Select
`64.times{|v|96.times{|u|print((u*u+v*v)**0.5%1==0?"o":" ")}puts}`

This is a 65-byte solution in Ruby. You're supposed to paste that in a file and then just run it, that's it.

It prints a beautiful ASCII art thing:

`ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo                                                                                               o                                                                                               o   o                                                                                           o  o                                                                                            o           o                                                                                   o       o                                                                                       o                       o                                                                       o     o        o                                                                                o           o                           o                                                       o                       o                                                                       o                                                           o                                   o    o   o      o                  o                                                            o                                                                                   o           o                                               o                                               o       o           o               o                                                           o           o                 o                                o                                o                                                                                               o                       o                                                       o               o                                                                                               o              o     o                          o                                               o                   o       o                                           o                       o                                                                                               o                                                                                               o      o  o       o             o            o                        o                         o                                                           o                                   o                                                                                               o                                   o                                                           o                    o                       o                                                  o                                                                                               o               o                       o                               o                       o                                                                                               o                       o                                   o                                   o                                           o           o                                       o                                                                                               o           o                                                                       o           o              o           o                    o                            o                  o                                                                                               o                                                                                               o                                                   o                           o               o        o                    o           o                                o                    o                                                                                               o                                       o               o                                       o                                                                                               o                                o                                                              o                       o   o                               o                                   o                                                                                               o                                                                                               o             o     o               o                  o        o                         o     o                                                                                               o                                                                                               o                                                                   o                           o                                      o                                                        o                                                                                               o                                                                       o                       o                                               o                                               o                                o        o                                               o     o                                                                           o                   o                                                                                               o                                                                                               o          o             o      o            o                 o                o          o    o                                                                                               o                                                                                               o               o                                           o                       o           `

Bonus:
Code (Ruby) Select
`a=064.times{|v|96.times{|u|a+=1if(u*u+v*v)**0.5%1==0}}p a`
Code (Ruby) Select
`p (0..6143).select{|n|((n%96)**2+(n/96)**2)**0.5%1==0}.size`

Both 59 bytes in Ruby, gives out 249.
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader --Snektron

if you wanna throw money at me and/or CodeWalrus monthly it's here

#### kotu

##### August 16, 2017, 09:27:11 am #11
Thanks
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### Snektron

##### August 16, 2017, 11:41:34 am #12
`#define mainImage(O, U)O+=sign(fract(length(U-.5)))`
50 chars / 51 bytes
its bigger than 64x96 though so you'll just have to resize your browser.
Legends say if you spam more than DJ Omnimaga, you will become a walrus...

#### kotu

##### August 16, 2017, 11:54:42 am #13 Last Edit: August 16, 2017, 12:01:50 pm by kotu
We all know it is impossible to tell if sqrt(x²+y²) is an integer or not, right?

Only humans can do that, really

..hmm
probably the best thing to do is store bools in a lookup table, start with 3x4, multiply that out (6x8, 9x12 etc)
bleah!

as a double blow i think the sqrt function is probably a numerical method
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### _iPhoenix_

##### August 16, 2017, 02:17:10 pm #14
Uhh it is not impossible.