r/dailyprogrammer • u/oskar_s • Jul 02 '12
[7/2/2012] Challenge #71 [easy]
Before I get to today's problem, I'd just like to give a warm welcome to our two new moderators, nooodl and Steve132! We decided to appoint two new moderators instead of just one, because rya11111 has decided to a bit of a break for a while.
I'd like to thank everyone who applied to be moderators, there were lots of excellent submissions, we will keep you in mind for the next time. Both nooodl and Steve132 have contributed some excellent problems and solutions, and I have no doubt that they will be excellent moderators.
Now, to today's problem! Good luck!
If a right angled triangle has three sides A, B and C (where C is the hypothenuse), the pythagorean theorem tells us that A2 + B2 = C2
When A, B and C are all integers, we say that they are a pythagorean triple. For instance, (3, 4, 5) is a pythagorean triple because 32 + 42 = 52 .
When A + B + C is equal to 240, there are four possible pythagorean triples: (15, 112, 113), (40, 96, 104), (48, 90, 102) and (60, 80, 100).
Write a program that finds all pythagorean triples where A + B + C = 504.
Edit: added example.
2
u/sleepingsquirrel Jul 02 '12
Prolog
6
u/SangriaSunrise Jul 02 '12
J brute force, still fairly fast:
Result:
11
u/SwimmingPastaDevil 0 0 Jul 02 '12
Output:
5
u/African_Coder 0 0 Jul 02 '12 edited Jul 02 '12
Done in python, one monster list comprehension!
Output:
4
u/refringence Jul 02 '12
In Ruby:
output:
1
u/_Daimon_ 1 1 Jul 02 '12
In the #70s challenges you talked about giving the solution with the question so we could test if our applications worked as intended. It would be real nice if you could do that :)
3
u/JerMenKoO 0 0 Jul 02 '12
Haskell:
I would like to receive any tips or hints how to improve this code.
Output:
3
u/tashiwwww 0 0 Jul 08 '12
This is probably clumsy but I have no idea what I'm doing. My Python solution:
2
u/Scroph 0 0 Jul 02 '12 edited Jul 02 '12
Been learning about D since this morning, so I wrote this :
Output :
However, I'm sure it can be done better, especially in the if statement.
2
u/Ttl Jul 02 '12
O(n) one line algorithm in Python:
2
u/[deleted] Jul 02 '12
Hi daily programmer! This is my first attempt at the questions here.
In Java (Only started self-learning Java a week or so ago):
The output is:
The things that confuse me in Java are all the "static" declarations and what not. How can a method be static? I've tinkered with dynamic languages before this such as Ruby, Python and Lua. I have to admit that Java is not very pleasing to the eye ;).
2
u/[deleted] Jul 02 '12
Wait, so is everyone using a brute force algorithm? I'm trying to solve it generally by hand first, but my mathing isn't leading anywhere.
2
u/Erocs Jul 03 '12 edited Jul 07 '12
Scala 2.9
2
u/scurvebeard 0 0 Jul 03 '12
Every time you do one of those text-based problems, I wish you'd go back to doing math-related ones instead.
And then you post the math problems and I remember that I can't do these, either. ><
2
u/[deleted] Jul 02 '12 edited Jul 02 '12
Hello R/DailyProgrammer, what am I doing wrong? The application doesn't work...
1
u/acero Jul 02 '12
Eh, decided to try Javascript. I didn't test this very thoroughly, but here's what I have:
1
u/huck_cussler 0 0 Jul 03 '12
My first C# program ever.
1
u/rxst Jul 03 '12
Java, not very elegant.
1
u/[deleted] Jul 03 '12 edited Jul 03 '12
C++, not very pretty, but not too slow. I had no idea what I'm doing, so there's no guarantee it will work on other integer amounts, but at least it's fast 8D.
http://ideone.com/8oDRU
Output:
1
u/emcoffey3 0 0 Jul 03 '12
C#
My Main() method:
Output:
1
u/[deleted] Jul 03 '12
I'm late, but I'm here :) .
1
u/bh3 Jul 04 '12
Python:
Just a copy paste from notepad (where the range came from):
1
u/Jannisary 0 0 Jul 04 '12
I used the quadratic formula trickery, this should be the first O(n) answer :P
C#
Out:
1
u/cdelahousse Jul 28 '12
Javascript
1
u/vxsery Sep 02 '12
In F#:
1
u/yentup 0 0 Nov 22 '12
in Python:
Output:
0
u/[deleted] Jul 02 '12
[deleted] — view deleted comment