পাইথাগোরিয়ান উপপাদ্যটি প্রয়োগ করার সবচেয়ে সংক্ষিপ্ত পদ্ধতি


14

পাইথাগোরিয়ান উপপাদ্য কে + বি² = সি² ভালবাসেন না? আপনি যেকোন ভাষায় স্বল্পতম পদ্ধতিটি লিখুন যা ক এবং খ এর মান নেয় এবং "এই ডান ত্রিভুজের অনুভূতি হ'ল" + সি। কেবল তিন দশমিক স্থানে সি রাখুন।


3
এটি কি প্রোগ্রামিং ধাঁধা হিসাবে যোগ্যতা অর্জন করে?
ডেভিডসি

1
@ ডেভিডকারারহর প্রতি সে সমস্যাটি কোনও প্রোগ্রামিং ধাঁধা নয়। তবে যেহেতু উদ্দেশ্য হ'ল এটির জন্য কোনও সমাধান গল্ফ করা, তবে এটি আসলে প্রোগ্রামিং ধাঁধা।
ভিক্টর স্টাফুসা

3
স্বল্পতম চরিত্রগুলি
ভিক পি

4
code-golfট্যাগ স্পষ্টভাবে বলছেন "কোড-গলফ অল্পসংখ্যক পরিষেবা মধ্যে একটি নির্দিষ্ট সমস্যা সমাধানের জন্য একটি প্রতিযোগিতার হয় বাইট উত্স কোডের।" দেখুন স্কোরিং কোড গলফ (বাইটস বনাম অক্ষর)
মাঝামাঝি

1
@ রেস: ফিক্সড :
টিমউই

উত্তর:


15

এপিএল (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 থেকে দশমিক স্থানে

এটি অপরাজেয় সম্পর্কে
ক্রুঙ্কার

@ ক্রাঙ্কার: আমি স্ট্রিংটি এনকোড করার চেষ্টা করেছি কিন্তু ডিকোডিং রুটিনটি যথেষ্ট ছোট পাই না।
মেরিনাস

কবুতরের গর্তের নীতি অনুসারে, আমি মনে করি আপনার কাছে খুব কঠিন সময় হবে (সম্ভবত অসম্ভব। স্ট্রিংটিতে কিছু লজিকাল প্যাটার্ন থাকলে সম্ভবত এটি প্রদর্শিত হয় না। আপনার এতদূর পর্যন্ত প্রচেষ্টাটি দেখতে আগ্রহী আমি
ক্রંચার

7
একটি অক্ষর সংরক্ষণ করতে "অনুমান" এর বানানটি সংশোধন করুন।
টিম এস

1
@ ক্রাঞ্চার: আসলে, আমি এটিকে স্ক্রিপ্টিংয়ে পরাজিত করেছি ...
টিমউই

11

টিআই-বেসিক, 76 55 53 52 বাইট

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

না, একটি ক্লোজিং বন্ধনী প্রয়োজন হয় না। এছাড়াও, এপিএল উত্তরের চেয়ে কম বাইট :)


1
হতে পারে R▶Pr(A,B
lirtosiast

3
একটি পরামর্শ কার্যকর করতে 2 বছর, LOL।
mbomb007

মহিমা জন্য নির্ধারিত, আমি অনুমান। এবং আমি এপিএলকে হারিয়ে আরও দুটি বাইট ছেঁটেছি!
টিমটেক


Inputজিজ্ঞাসা করতে ব্যবহার করা Xএবং Yএকধরনের হাসিখুশি। যদি আমরা এটির অনুমতি দিই, তবে কি আমাদেরও PolarGCআগে সেটিংয়ের অনুমতি দেওয়া উচিত Input, যাতে হাইপোথেনজটির দৈর্ঘ্য এক-বাইট দ্বারা দেওয়া হয় R? বোঝা যাচ্ছে যে নেতারা, সঙ্গে PolarGCমান Xএবং Yআর প্রদর্শিত যখন আমরা কার্সার প্রায় সরাতে, কিন্তু তারা এখনও সঞ্চিত উপযুক্ত ভেরিয়েবল করতে। (যা আমরা তখন কখনই ব্যবহার করব না, তবে এটিই মনে করা যায়))
মিশা লাভরভ

9

পাইথন 2.7 - 76 টি অক্ষর

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

ব্যাখ্যা

| একটি + আইবি | = √ (একটি 2 + বি 2) ) = গ

==> a 2 + b 2 = c 2

হাইপোথেনুজে পিজে

শিক্ষক: "আপনি আমাকে বলতে পারেন, হাইপোপেনজ কী?"

এলজে: "হাইপোটেনিউজ, একটি সহজ প্রশ্ন last গত রাতে যদি কোনও হাই প্রোফাইল পার্টি থাকে, এবং আপনি নিউজ পেপারে এটি পড়েন, যার নাম হাই পার্টি News"


8

স্ক্লিপটিং , 46 টি অক্ষর

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

স্থান দ্বারা পৃথক দুটি সংখ্যা (ভগ্নাংশ হতে পারে!) হিসাবে ইনপুটটি প্রত্যাশা করে।

এটি কয়েকটি অসুবিধাজনক কৌশল ব্যবহার করেও এপিএল থেকে কম।

ব্যাখ্যা

글坼 | 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
ইউনিকোড এটি 92 বাইটের মতো করে না?
ক্রંચার

@ ক্র্যাঞ্চার আমি গতকাল প্রশ্ন মন্তব্যে জিজ্ঞাসা করেছি, বাইট গণনা নয়, চরিত্র গণনা কী গণনা করা হয়।
জোচিম ইসাকসন 21

3

ডিসি 54

এপিএলের স্কোরের জবাব!

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" 000
টমাস

1
@ টমাস এটি আ আ ফাংশন ধরণের; আপনাকে প্রথমে স্ট্যাকের উপর প্যারামিটারগুলি রাখা দরকার, যেমন আমি পরীক্ষায় দেখায় বা আপনি যদি নিজের পথে যেতে চান তবে এটি হবে dc -e '3 4 2^r2^+3kv[...যেখানে 3 এবং 4 পরামিতি রয়েছে।
দানিরো

3

সি, 77 বা 99

যদি ইনপুট কেবল ফাংশন আর্গুমেন্ট হতে পারে তবে 77 টি অক্ষর:

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

99 যদি স্ট্যান্ডিনের কাছ থেকে ইনপুটটি পড়তে হবে:

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

@ ইয়ামিন রংকে একটি বিশাল ধন্যবাদ!


1
একটি অনুমান (ক, খ) রয়েছে যা আপনাকে তিনটি অক্ষর সাশ্রয় করবে।

এই ফাংশনটি আমি টিসিওতে জিসিসি টিসিসি ক্ল্যাং ব্যবহার করি না কেন তা সংকলন করে না ... সম্ভবত একটি # শিরোনাম এবং শিরোনামে সংকলকের সঠিক সংস্করণটির অভাব রয়েছে
রোজলুপি

3

শক্তির উৎস

আমি দেখতে পারতাম কিনা তা দেখার জন্য ...

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
চমৎকার কাজ. দেখে মনে হচ্ছে আপনি নিজের সমাধানে ব্যস্ত থাকাকালীন আপনি এটি করেছেন which যা প্রায় 62 টি চরিত্রের দ্বারা এটি প্রহার করে। ভবিষ্যতের রেফারেন্সের জন্য, কোড গল্ফের উত্তরগুলি "গল্ফড" এবং তাদের "স্কোর" অন্তর্ভুক্ত থাকবে বলে আশা করা হচ্ছে। "গল্ফিং" এর অর্থ হ'ল সংক্ষিপ্ত হাতের এলিয়াস, সিনট্যাক্স কৌশল এবং ভাষার নিয়ম প্রসারিত করার অন্যান্য উপায় ব্যবহার করে চরিত্রের দৈর্ঘ্য হ্রাস করার জন্য আপনার সর্বাত্মক প্রচেষ্টা করা উচিত। আপনার যেখানে প্রয়োজন সেখানে অপ্রয়োজনীয় সাদা স্থানও সরিয়ে ফেলতে হবে (কমপক্ষে তিনটি স্পেস রয়েছে যা আপনার সমাধান থেকে সরানো যেতে পারে)। এই ক্ষেত্রে, "স্কোর" হ'ল আপনার চরিত্রের সংখ্যা - বর্তমানে 173
Is

কোড গল্ফ ট্যাগ উইকি, হেল্প সেন্টারের বিভিন্ন অংশ এবং পাওয়ারশেল থ্রেডের জন্য গল্ফিং টিপস কোড গল্ফ চ্যালেঞ্জগুলির প্রতিযোগিতামূলক উত্তর কীভাবে লিখতে হয় তার জন্য আরও ভাল অনুভূতি পেতে আমি আপনাকে পরামর্শ দেওয়ার পরামর্শ দিই।
ইসজি

এছাড়াও, আমি আপনার স্ক্রিপ্টের সাথে একটি ত্রুটি পেয়েছি। শুভেচ্ছা সঙ্গে "... প্যারামিটার নাম 'P' দ্ব্যর্থক ..." Read-Host
ইসজি

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
আপনি যদি দীর্ঘগুলির a**0.5পরিবর্তে ব্যবহার করেন তবে কয়েকটি অক্ষর সংরক্ষণ করতে পারেন Math.sqrt(a)। এবং পরে স্থানও pসরানো যেতে পারে।
নিক ও'লাই

1
এবং আপনার মধ্যে প্রথম বন্ধনী প্রয়োজন নেই %(Math...)
নিক ও'লাই

2

ম্যাটল্যাব 79 74

@(a,b)sprintf('The hypotenuse of this right triangle is %.3f',norm([a b]))


2

সি ++ - 90

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

pow(a,2)আপনি কখন করতে পারেন a*a? আমিও আছি না নিশ্চিত আমি 1000 দ্বারা তলার উদ্দেশ্য এবং +.5 এবং সংখ্যাবৃদ্ধি এবং ডিভাইড বুঝতে
Cruncher

@ ক্রুনচার ফ্লোরটি দশমিক স্থানটি .3 জায়গায় নির্ধারণ করতে হবে। আমি এখনই এটি পুনরায় কাজ করছি, এবং আপনার পরামর্শ অন্তর্ভুক্ত করব।

একটি অনুমান (ক, খ) রয়েছে যা আপনাকে তিনটি অক্ষর সাশ্রয় করবে।

পছন্দ করুন

2

পার্ল 6 (68 74 বাইট)

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

{}একটি ল্যাম্বদা ফাংশন ঘোষণা করে। [+]যোগফল অপারেটর, X**ক্রস পাওয়ার অপারেটর (উদাহরণস্বরূপ, 1, 2 X+ 10, 20দেয় 11, 21, 12, 22)। এই ক্ষেত্রে, ক্রস ক্ষমতা অপারেটর এক যুক্তি লাগে, তাই এর ফলে একই দৈর্ঘ্য @_@_সমস্ত ফাংশন আর্গুমেন্ট রয়েছে।

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

Javascript (97)

x=prompt;a=x(),b=x();x('The hypotenuse of this right triangle is '+Math.sqrt(a*a+b*b).toFixed(3))

.toFixed .. thank you! learned something new :)
micha

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

2

Python 2 (79)

def p(a,b):print'The hypotenuse of this right triangle is %.3d'%((a*a+b*b)**.5)

Dispense with math for some savings. (a*a+b*b)**.5
Steven Rumbalski

Since the body of your function is a single statement, it can be on the same line as the def saving a newline and an indent.
Steven Rumbalski

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

ARBLE, 73 bytes

"The hypotenuse of this right triangle is "..floor(sqrt(a^2+b^2)*1e3)/1e3

Try it online!


1
@H.PWiz I must say, for a challenge that should just be sqrt(a^2+b^2), this has a lot of unnecessary boilerplate.
ATaco

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.