วิธีที่สั้นที่สุดในการใช้ทฤษฎีบทพีทาโกรัส


14

ใครไม่รักทฤษฎีบทพีทาโกรัสที่² + b² = c²? เขียนวิธีที่สั้นที่สุดที่คุณสามารถทำได้ในภาษาใด ๆ ที่มีค่า a และ b และพิมพ์ออกมา "ด้านตรงข้ามมุมฉากของสามเหลี่ยมมุมฉากนี้คือ" + c เก็บ c ถึงทศนิยมสามตำแหน่งเท่านั้น


3
สิ่งนี้มีคุณสมบัติเป็นปริศนาการเขียนโปรแกรมหรือไม่?
DavidC

1
@DavidCarraher ปัญหาต่อ se ไม่มีการเขียนโปรแกรมปริศนา แต่เนื่องจากวัตถุประสงค์คือการแก้ปัญหาสำหรับกอล์ฟแล้วมันเป็นปริศนาการเขียนโปรแกรม
Victor Stafusa

3
ตัวละครที่สั้นที่สุด
Vik P

4
code-golfแท็กอย่างชัดเจนกล่าวว่า"รหัสกอล์ฟคือการแข่งขันที่จะแก้ปัญหาที่เกิดขึ้นโดยเฉพาะอย่างยิ่งในน้อยไบต์ของรหัสที่มา." ดูเกณฑ์การให้คะแนนกอล์ฟรหัส (ไบต์เทียบกับตัวอักษร)
res

1
@res: แก้ไข :-p
Timwi

คำตอบ:


15

APL (54)

'The hypotenuse of this right triangle is',3⍕.5*⍨+/⎕*2

ทดสอบ:

      'The hypotenuse of this right triangle is',3⍕.5*⍨+/⎕*2
⎕:
      9 10
The hypotenuse of this right triangle is 13.454

คำอธิบาย:

  • ⎕*2: เพิ่มค่าในอินพุตเป็นกำลังสอง
  • +/: รับผลรวม
  • .5*⍨: เพิ่มผลลัพธ์เป็นพลังที่ 0.5
  • 3⍕: ปัดทศนิยมทศนิยม 3 ตำแหน่ง

นี่เป็นเรื่องที่ไม่สามารถเอาชนะได้
Cruncher

@ Cruncher: ฉันพยายามเข้ารหัสสตริง แต่ไม่สามารถถอดรหัสรูทีนขนาดเล็กได้
marinus

ตามหลักการของนกพิราบฉันคิดว่าคุณมีช่วงเวลาที่ยากมาก (อาจเป็นไปไม่ได้มันต้องเป็นไปไม่ได้สำหรับบางสาย) พยายามบีบอัดสาย อาจเป็นไปได้ว่าสตริงนั้นมีรูปแบบเชิงตรรกะ แต่ก็ไม่เป็นเช่นนั้น ฉันสนใจที่จะเห็นความพยายามที่คุณเคยทำมา
Cruncher

7
แก้ไขการสะกดของ "ด้านตรงข้ามมุมฉาก" เพื่อบันทึกอักขระ
ทิมเอส

1
@ Cruncher: จริง ๆ แล้วฉันชนะมันใน Sclipting ...
Timwi

11

TI-BASIC, 76 55 53 52 ไบต์

Input :Disp "THE HYPOTENUSE OF THIS RIGHT TRIANGLE IS
Fix 3:R▶Pr(X,Y

ไม่จำเป็นต้องใช้วงเล็บปิด นอกจากนี้ไบต์น้อยกว่า APL คำตอบ :)


1
R▶Pr(A,Bอาจจะเป็น
lirtosiast

3
2 ปีในการดำเนินการตามคำแนะนำ lol
mbomb007

ฉันเดาว่าเป็นจุดหมายปลายทางที่ยิ่งใหญ่ และฉันเพิ่งโกนอีกสองไบต์เพื่อเอาชนะ APL!
Timtech

-1 ไบต์:Fix 3:R►Pr(X,Y
Khuldraeseth na'Barya

ใช้Inputเพื่อขอXและYเป็นประเภทเฮฮา ถ้าเราอนุญาตสิ่งนั้นเราควรอนุญาตให้ตั้งไว้PolarGCก่อนInputเช่นกันเพื่อให้ความยาวของด้านตรงข้ามมุมฉากถูกกำหนดโดยหนึ่งไบต์R? เป็นที่ยอมรับด้วยPolarGCค่าของXและYจะไม่ปรากฏอีกต่อไปเมื่อเราเลื่อนเคอร์เซอร์ไปรอบ ๆ แต่พวกเขายังคงเก็บไว้ในตัวแปรที่เหมาะสม (ซึ่งเราจะไม่ใช้ แต่มันเป็นความคิดที่นับ)
Misha Lavrov

9

Python 2.7 - 76 ตัวละคร

print'The hypotenuse of this right triangle is %.3f'%abs(input()+1j*input())

คำอธิบาย

| A + IB | = √ (a 2 + b 2 ) = c

==> a 2 + b 2 = c 2

PJ กับด้านตรงข้ามมุมฉาก

ครู: "คุณบอกฉันได้ไหมด้านตรงข้ามมุมฉากคืออะไร"

LJ: "Hypotenuse เป็นคำถามง่าย ๆ ถ้ามีงานปาร์ตี้ที่โด่งดังเมื่อคืนนี้และคุณอ่านมันในกระดาษข่าวเรียกว่า High Party News"


8

Sclipting 46 ตัวอักษร

글坼各갠方終加감半方갾밈乘增貶껠矽녆둥긆둹댆뭴뉖멵댶넠닶눠덆둩댲걲늖덨덂건댦땡닦덬뉒걩댲밀⓶

คาดว่าอินพุตเป็นตัวเลขสองตัว (สามารถแยกได้!) คั่นด้วยช่องว่าง

สิ่งนี้สั้นกว่า APL แม้จะต้องใช้ลูกเล่นที่ไม่สะดวก

คำอธิบาย

글坼 | split at space
各 | for each...
  갠方 | to the power of two
終
加 | add
감半方 | to the power of one half
갾밈乘 | multiply by 1000
增貶 | increment, then decrement (kludge for rounding)
껠矽 | insert '.' at 4th-last character position
녆둥긆둹댆뭴뉖멵댶넠닶눠덆둩댲걲늖덨덂건댦땡닦덬뉒걩댲밀⓶ | "The hypotenuse..."

2
ไม่ทำ Unicode ให้ขนาดนี้เป็น 92 ไบต์?
Cruncher

@Cruncher ฉันถามเมื่อวานนี้ในคำถามความคิดเห็นสิ่งที่นับคือการนับตัวอักษรไม่นับไบต์
Joachim Isaksson

3

dc 54

แสดงคะแนนของคำตอบ APL!

2^r2^+3kv[The hypotenuse of this right triangle is ]Pp

ทดสอบ:

$ dc
3 4
2^r2^+3kv[The hypotenuse of this right triangle is ]Pp
The hypotenuse of this right triangle is 5.000

ไม่ได้ผลสำหรับฉันจริงๆ dc -e '2^r2^+3kv[The hypotenuse of this right triangle is ]Pp'ไม่ต้องรออินพุตใด ๆ พิมพ์"dc: stack empty"3 ครั้งจากนั้น "ด้านตรงข้ามมุมฉากของสามเหลี่ยมมุมฉากนี้คือ 2.000"
โทมัส

1
@ Tomas มันเรียงลำดับของฟังก์ชั่น aa; คุณต้องใส่พารามิเตอร์ใน stack ก่อนเช่นฉันแสดงในการทดสอบหรือถ้าคุณต้องการเรียกวิธีของคุณมันจะเป็นdc -e '3 4 2^r2^+3kv[...ที่ 3 และ 4 เป็นพารามิเตอร์
daniero

3

C, 77 หรือ 99

77 อักขระหากอินพุตสามารถเป็นอาร์กิวเมนต์ของฟังก์ชันได้:

f(a,b){printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}

99 ถ้าอินพุตต้องอ่านจาก stdin:

a,b;f(){scanf("%d %d",&a,&b);printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}

ขอบคุณมากที่ @Yimin Rong!


1
มี hypot (a, b) ซึ่งจะช่วยให้คุณสามอักขระ

ฟังก์ชั่นนั้นไม่ได้คอมไพล์อะไรก็ตามที่ฉันใช้ gcc tcc clang ใน tio ... อาจจะมีส่วนหัว #include หนึ่งอันและในชื่อรุ่นที่ถูกต้องของคอมไพเลอร์
RosLuP

3

Powershell

เพียงเพื่อดูว่าฉันสามารถ ...

echo "The hypotenuse of this right triangle is " ([math]::round([math]::sqrt(([math]::pow(([double](Read-Host -p "A")),2) + [math]::pow(([double](Read-Host -p "B")),2))),3))

1
ทำได้ดีมาก ดูเหมือนว่าคุณจะทำสิ่งนี้ในขณะที่ฉันกำลังยุ่งกับทางออกของตัวเองซึ่งเต้นประมาณ 62 ตัวอักษร สำหรับการอ้างอิงในอนาคตคำตอบของ Code Golf คาดว่าจะเป็น "golfed" และมี "คะแนน" รวมอยู่ด้วย "การตีกอล์ฟ" หมายความว่าคุณควรใช้ความพยายามทุกวิถีทางเพื่อลดความยาวของตัวละครโดยการใช้นามแฝงมือสั้น, เทคนิคการซิงก์และวิธีการอื่น ๆ ในการยืดกฎของภาษา คุณควรลบช่องว่างที่ไม่จำเป็นหากเป็นไปได้ (มีช่องว่างอย่างน้อยสามช่องที่สามารถลบออกได้จากโซลูชันของคุณ) "คะแนน" ในกรณีนี้คือการนับตัวละครของคุณ - ปัจจุบัน 173.
Iszi

ฉันขอแนะนำให้อ่านรหัสวิกิกอล์ฟแท็กส่วนต่างๆของศูนย์ช่วยเหลือและเคล็ดลับการเล่นกอล์ฟสำหรับ PowerShell เพื่อให้ได้ความรู้สึกที่ดีขึ้นสำหรับวิธีการเขียนคำตอบที่แข่งขันกับความท้าทายในการเขียนโค้ดกอล์ฟที่นี่
Iszi

นอกจากนี้ฉันได้รับข้อผิดพลาดกับสคริปต์ของคุณ "... ชื่อพารามิเตอร์ p '' ไม่ชัดเจน ..." Read-Hostเกี่ยวกับการ
Iszi

3

ทับทิม, 94 90 82 ตัวอักษร

p "The hypotenuse of this right triangle is %.3f"%(Math.sqrt(gets.to_i**2+gets.to_i**2))

อัปเดต (ขอบคุณสำหรับความคิดเห็น):

p "The hypotenuse of this right triangle is %.3f"%(gets.to_i**2+gets.to_i**2)**0.5

1
You could save a few chars if you use a**0.5 instead of lengthy Math.sqrt(a). And the space after p can also be removed.
Nik O'Lai

1
And you do not need parenthesis in %(Math...).
Nik O'Lai



2

C++ - 90

void h(int a,int b){printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}

pow(a,2) when you can do a*a? I'm also not sure I understand the purpose of the floor and the +.5 and the multiply and divide by 1000
Cruncher

@Cruncher The floor is to set the decimal place to .3 places. I am reworking it right now, and will include your suggestion.

There is a hypot(a,b) which will save you three characters.

@YiminRong Cool!

2

Perl 6 (68 74 bytes)

{printf "The hypotenuse of this right triangle is %.3f
",sqrt [+] @_ X**2}

{} declares a lambda function. [+] is sum operator, X** is cross power operator (for example, 1, 2 X+ 10, 20 gives 11, 21, 12, 22). In this case, cross power operator takes one argument, so the result has the same length as @_. @_ contains all function arguments.

If it's disallowed to have function that may take wrong number of arguments (unsafe), it's possible to replace [+] @_ X**2 with $^a**2+$^b**2, where $^a and $^b are placeholder arguments.


2
How would you limit to 3 decimal places?
Joachim Isaksson

@JoachimIsaksson: I fail at reading. Should be fixed now.
Konrad Borowski


2

C, 100 chars (beats the other C solution by 1!)

A ridiculously inefficient algorithm.

x;f(a,b){for(;x-a*a-b*b;x=rand());printf("The hypotenuse of this right triangle is %.3f",sqrt(x));}

Sorry, but you've written "the" instead of "this", so if you correct that it's the same length ;P
daniero

@daniero Ok, found a fix, now still one char down :)
user12205

In the Borland C compiler it would not compile...
RosLuP

2

DELPHI / PASCAL

With indent (157)

program p;
{$APPTYPE CONSOLE}
var a,b:integer;
begin
     readln(a,b);
     writeln('the hypotenuse of this right triangle is',sqrt(b*b+a*a):2:3);
end.

1
ah man, i had the exact same :(
Teun Pronk

to late for an edit, so again.. Edit: You can get 2 characters off by changing integer to int16 You dont have to include the first 2 lines for your answer, and you can remove whitespace. doing all that gives you 106 characters.
Teun Pronk

2

EcmaScript 6, 82 79

f=(a,b)=>"The hypotenuse of this right triangle is "+Math.hypot(a,b).toFixed(3)

Usage:

f(3, 5)
> "The hypotenuse of this right triangle is 5"

Update: Switch to Math.hypot()


2

Golfscript (69 67 66 65)

This would be much easier if floating point was actually supported without resorting to workarounds... :)

~'The hypotenuse of this right triangle is '@.*@.*+2-1??+.'.'?4+<

A link to test it.


why are you doing 2.!~ when 2-1 is shorter?
McKay

@McKay Good question, I always getthe difference between 2- 1` and 2-1 wrong, so was probably temporarily confused :) Fixed, thanks.
Joachim Isaksson


1

AWK — 84 78 characters

awk '{printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}'

Thanks to Wasi for suggesting ^ operator and removing ()!

e.g.

$ echo 3 4 | awk '{printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}'
The hypotenuse of this right triangle is 5.000

You can golf it further {printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}
Wasi

1

PowerShell: 111

Golfed Code

1..2|%{sv $_ (read-host)};"The hypotenuse of this right triangle is $("{0:N3}"-f[math]::sqrt($1/1*$1+$2/1*$2))"

Walkthrough

1..2|%{sv $_ (read-host)}; Gets two inputs interactively from the user, and stores them in $1 and $2. Might be able to cut some length by using arguments or pipeline inputs instead.

"The hypotenuse of this right triangle is Required text in the output, per the challenge specifications.

$(...)" Encapsulated code block will be processed as script before being included in the output.

"{0:N3}"-f Formats output from the next bit of code as a number with exactly three digits after the decimal point.

[math]::sqrt(...) Gets the square root of the encapsulated value.

$1/1*$1+$2/1*$2 Serves as our "a^2+b^2". Multiplying a number by itself is the shortest way to square it in PowerShell, but the variables need to be divided by 1 first to force them to integers. Otherwise, they are treated as text and 3*3+4*4 would be 3334444 instead of 25.


1

JavaScript: 83

i=prompt,'The hypotenuse of this right triangle is '+Math.hypot(i(),i()).toFixed(3)

Currently the shortest JS implementation using stdin :D
Works only on Firefox 27.0+ (EcmaScript 6)

JavaScript: 78

If we can use just two variables (as lot of scripts do here):

a=2,b=3,'The hypotenuse of this right triangle is '+Math.hypot(a,b).toFixed(3)

whoooo .. .hypot. Good catch!
micha


1

Java, 112

(Also prints out a No Such Method error, though I'm not sure if this is against the rules)

class A{static{int a=1,b=1;System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(a,b));}}

Java, 149

(No error)

class A{static{int a=1,b=1;System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(a,b));}public static void main(String[] a){}}

1

C#

Method Only (114)

void H(double a, double b)
{
    Console.Write("The hypotenuse of this right triangle is {0:N3}", Math.Sqrt(a * a + b * b)); 
}

Complete Program (171)

using System;
class P
{        
   static void H(double a, double b)
   {
     Console.Write("The hypotenuse of this right triangle is {0:N3}", Math.Sqrt(a * a + b * b));
   }                
   static void Main()
   {
    H(3, 4);
   }
}

Complete Program (without using method - 141)

using System;class P{static void Main(){double a=3,b=4;Console.Write("The hypotenuse of this right triangle is {0:N3}",Math.Sqrt(a*a+b*b));}}

1
Heres my complete program. 141 chars...you can save a few chars using the formatstring overload of write using System;class P{static void Main(){double a=3,b=4;Console.Write("The hypotenuse of this right triangle is {0:N3}",Math.Sqrt(aa+bb));}}
Tim Bailey

1
You can use string formatting in the Console.Write instead of calling ToString() to save 9 characters.
Rik

1

JavaScript 118 106 93

Unlike @micha's solution, mine takes in two variables via function and sends the alert of the result.

function(a,b){m=Math;c=d=>d*d,e=1e3;alert("The hypotenuse of this right triangle is "+m.round(m.sqrt(c(a)+c(b))*e)/e)}

function(a,b){e=1e3;alert("The hypotenuse of this right triangle is "+Math.round(Math.sqrt(a*a+b*b)*e)/e)}

Fat arrow functions to the rescue!

h=(a,b,e=1e3)=>"The hypotenuse of this right triangle is "+Math.round(Math.sqrt(a*a+b*b)*e)/e


1
Could be shorter if you inline c(). Aliasing Math doesn't save bytes in your case.
Florent

@Florent Ah, yes... one sec!
WallyWest


1

R, 61 76 bytes

cat("The hypotenuse of this right triangle is",round(sqrt(sum(scan()^2)),3))

cat displays its content to STDOUT.

The scan() function takes user's input from keyboard. This input exists as a vector, on which the ^2 is applied (^function is vectorized), and the sum() sums the elements of the vector. sqrt outputs the square-root, which is rounded to 3 decimal places by round(,3)

Thanks to @caird coinheringaahing for noticing that the previous answer didn't round.


Does this "Keep c to only three decimal places."?
caird coinheringaahing

@cairdcoinheringaahing : it does now ! Thanks !
Frédéric


1

OML, 57 bytes

"The hypotenuse of this right triangle is "shnhn+A6`*N3eD

Try it online!

Part 1

This simply outputs the string

"The hypotenuse of this right triangle is "s

Part 2

hnhn+A6`*N3eD
hn              take input and square it
  hn            take another input and square it
    +           add them
     A6`        push 10^6
        *       multiply the sum with that number
         N      take integer square root
          3eD   output with three places of precision
                implicit output

1

Jelly, 32 characters

,²S½ær3µ,“¡ÆC⁷⁺ɱSoṿȤç½?⁶Ẏtḍỵŀ»ṚK

Try it online!

There is probably a better string compression that allows me to get around needing to join with spaces but I was having trouble finding it.

Explanation:

,²S½ær3µ,“...»ṚK    Example inputs: 3, 4
,                   Pair the inputs. Result: [3, 4]
 ²                  Square them. Result: [9, 16]
  S                 Sum them. Result: 25
   ½                Get the square root of the sum. Result: 5
    ær3             Round to 3 decimal places. Result: 5
       µ            Take the result of that... Result: 5
         “...»       ...and  the compressed string Result: "The hypotenuse of this right triangle is"
        ,           And put them into a pair. Result: [5, "The hypotenuse of this right triangle is"]
              Ṛ     Reverse that. Result: ["The hypotenuse of this right triangle is", 5]
               k    Join it with spaces. Result: "The hypotenuse of this right triangle is 5.0"
                    Implicit output.

So long as only three decimal places means less than or equal to three decimal places, the output looks fine.
Engineer Toast
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.