You can help CodeWalrus stay online by donating here.

## Code Golf Contest: The Pythagorean Problem

Started by Yuki, August 15, 2017, 07:52:01 PM

0 Members and 1 Guest are viewing this topic.

#### Yuki

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.
• Consoles, mobile devices and vintage computers owned: A lot
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

#1
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?

#### Yuki

#2
Exactly. c a bit irrelevant as you can derive it from a and b, but yeah, pretty much.
• Consoles, mobile devices and vintage computers owned: A lot
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

Can I use C++
• Calculators owned: TI 84+CE-T
• Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### jamu

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

#5
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)
• Calculators owned: TI 84+CE-T
• Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### Yuki

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.
• Consoles, mobile devices and vintage computers owned: A lot
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

@Juju do you have Turbo C++ 2006? I might be able to make something pretty... meh
• Calculators owned: TI 84+CE-T
• Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### Yuki

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.
• Consoles, mobile devices and vintage computers owned: A lot
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

#9
I did it in * here is the code
`f(096){f(064)`

*C++Builder
• Calculators owned: TI 84+CE-T
• Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### Yuki

#10
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.
• Consoles, mobile devices and vintage computers owned: A lot
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

• Calculators owned: TI 84+CE-T
• Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### Snektron

`#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.
• Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...

#### kotu

#13
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
• Calculators owned: TI 84+CE-T
• Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

#### _iPhoenix_

Uhh it is not impossible.
• Calculators owned: Two TI-84+ CE's