实现勾股定理的最短方法


14

谁不喜欢勾股定理a²+b²=c²?用任何能用a和b取值并打印出“此直角三角形的斜边为+”的语言编写最短的方法。将c保留到小数点后三位。


3
这符合编程难题吗?
DavidC 2014年

1
@DavidCarraher问题本身并不是编程难题。但是由于目标是寻求解决方案,所以这确实是编程难题。
Victor Stafusa 2014年

3
最短的字符
Vik P 2014年

4
code-golf标签明确表示“代码高尔夫是在源代码最少字节中解决特定问题的竞赛。” 请参阅评分代码高尔夫球(字节与字符)
res

1
@res:固定:-p
Timwi,

Answers:


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⍕:舍入到小数点后三位

这是无与伦比的
Cruncher 2014年

@Cruncher:我尝试对字符串进行编码,但是无法使解码例程足够小。
marinus 2014年

根据鸽子洞的原理,我认为您很难压缩字符串(这可能是不可能的。至少对于某些字符串来说是不可能的)。也许该字符串具有某种逻辑模式,但事实并非如此。我很高兴看到您到目前为止所做的尝试
Cruncher 2014年

7
更正“斜边”的拼写以保存字符。
Tim S.

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年,大声笑。
mbomb007'9

我猜是注定要伟大的。而我又削减了两个字节来击败APL!
Timtech's

-1个字节:Fix 3:R►Pr(X,Y
Khuldraeseth na'Barya

使用Input索要XY是那种热闹。如果允许的话,是否还应该设置PolarGCbefore Input,以使斜边的长度由一个字节给出R?可以肯定的是,当我们移动光标时,不再显示PolarGC的值,但是它们仍存储在适当的变量中。(那时我们将永远不会使用,但这才是最重要的想法。)XY
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:“斜边,一个简单的问题。如果昨晚有一个备受瞩目的聚会,而您在报纸上读到了它,那就是High Party News”。


8

滑行,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 2014年

我昨天在问题注释中问了@Cruncher,什么才是字符数,而不是字节数。
Joachim Isaksson 2014年

3

直流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”。
2014年

1
@Tomas有点像一个函数;您需要先将参数放在堆栈上,就像我在测试中所展示的那样,或者如果您想调用自己的方式,则dc -e '3 4 2^r2^+3kv[...参数3和4就是其中的地方。
daniero 2014年

3

C,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));}

非常感谢@Yimin Rong!


1
有一个hypot(a,b),它将为您节省三个字符。

该函数无法编译我在tio中使用gcc tcc clang的任何编译...可能缺少一个#include标头,并且标题中缺少编译器的精确版本
RosLuP

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
辛苦了 好像您是在我忙于自己的解决方案时做到了这一点,这个解决方案击败了62个字符。供将来参考,Code Golf的答案应被“打高尔夫”,并包括其“得分”。“高尔夫”意味着您应尽一切努力通过使用速记别名,语法技巧和其他扩展语言规则的方法来减少字符长度。您还应该尽可能删除不必要的空格(可以从解决方案中删除至少三个空格)。在“分数”,在这种情况下,你的字符数-目前173
Iszi

我建议阅读代码高尔夫标签Wiki,帮助中心的各个部分以及PowerShell的高尔夫技巧,以更好地了解如何在此处为代码高尔夫挑战编写有竞争力的答案。
Iszi 2014年

另外,您的脚本出现错误。关于“ ...参数名称'p'是不明确的...” Read-Host
Iszi 2014年

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而不是longy,则可以节省一些字符Math.sqrt(a)。而且之后的空间p也可以删除。
Nik O'Lai 2014年

1
并且您不需要在中加上括号%(Math...)
Nik O'Lai 2014年

2

MATLAB 79 74

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


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)什么时候可以做a*a?我也不确定我是否了解底数和+.5以及乘以1000的目的
Cruncher

@Cruncher地板将小数位设置为.3。我现在正在对其进行修改,并将包含您的建议。

有一个hypot(a,b),它将为您节省三个字符。

@YiminRong酷!

2

Perl 6(68 74字节)

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

{}声明一个lambda函数。[+]是求和运算符,X**是交叉幂运算符(例如,1, 2 X+ 10, 20给出11, 21, 12, 22)。在这种情况下,交叉幂运算符采用一个参数,因此结果的长度与相同@_@_包含所有函数参数。

如果它不允许有可能会采取错误的参数数目(不安全的)功能,它可以代替[+] @_ X**2$^a**2+$^b**2,其中$^a$^b是占位参数。


2
您如何限制到小数点后三位?
约阿希姆·伊萨克森

@JoachimIsaksson:我阅读失败。现在应该修复。
Konrad Borowski

2

Java语言(97)

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

.toFixed ..谢谢!学到了新的东西:)
micha 2014年

2

C,100个字符(比其他C解决方案大1!)

一种荒谬的低效算法。

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

抱歉,但是您写的是“ the”而不是“ this”,因此,如果您更正它的长度是相同的; P
daniero 2014年

@daniero好的,找到了解决方法,现在仍然是一个字符:)
2014年

在Borland C编译器中,它不会编译...
RosLuP

2

德尔菲/帕斯卡

带缩进(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
啊,我有完全一样的东西:(
Teun Pronk 2014年

编辑:您可以通过更改integer为来减少2个字符。int16您不必在答案中包括前2行,也可以删除空格。完成所有操作后,您将获得106个字符。
Teun Pronk 2014年

2

的EcmaScript 6,82 79

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

用法:

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

更新:切换到Math.hypot()




1

AWK- 84 78个字符

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

感谢Wasi建议^运算符并删除()!

例如

$ 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

您可以进一步打高尔夫球{printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}
Wasi

1

PowerShell的:111

高尔夫球码

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

演练

1..2|%{sv $_ (read-host)};从用户以交互方式获取两个输入,并将它们存储在$ 1和$ 2中。可以通过使用参数或管道输入来缩短长度。

"The hypotenuse of this right triangle is 根据挑战规范,输出中的必需文本。

$(... )"封装的代码块在包含到输出中之前将作为脚本处理。

"{0:N3}"-f 将代码的下一位输出格式设置为小数点后三位数字。

[math]::sqrt(... )获取封装值的平方根。

$1/1*$1+$2/1*$2用作我们的“ a ^ 2 + b ^ 2”。在PowerShell中将数字本身相乘是最短的平方方法,但是必须先将变量除以1才能将其强制为整数。否则,它们将被视为文本,并且3 * 3 + 4 * 4将是3334444而不是25。


1

的JavaScript:83

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

当前使用stdin:D 的最短JS实现
仅在Firefox 27.0+(EcmaScript 6)上有效

的JavaScript:78

如果我们只能使用两个变量(很多脚本在这里使用):

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

呜呜呜呜 接得好!
micha 2014年

1

直流55

3k?d*?d*+v[The hypotenuse of this right triangle is ]Pp

1

爪哇,112

(也可以打印出No Such Method错误,尽管我不确定这是否违反规则)

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

爪哇,149

(没有错误)

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#

仅方法(114)

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

完成课程(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);
   }
}

完整程序(不使用方法-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
这是我完整的程序。141个字符...您可以使用System;类P {静态void Main(){double a = 3,b = 4; Console.Write(“此直角三角形的斜边是{0:N3}“,Math.Sqrt(a a + b b));}}
蒂姆·贝利

1
您可以在Console.Write中使用字符串格式,而不用调用ToString()保存9个字符。
里克2014年

1

的JavaScript 118 106 93

与@micha的解决方案不同,mine通过函数接受两个变量并发送结果警报。

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)}

胖箭头功能可以解救!

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


1
如果内联,则可能会更短c()。别名Math不能节省字节数。
Florent 2014年

@佛罗伦萨,是的,一秒钟!
WallyWest 2014年


1

R,61 76字节

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

cat 将其内容显示到STDOUT。

scan()功能从键盘获取用户输入。此输入作为向量存在,在其^2上应用(向量已向^量化),并对sum()向量的元素求和。sqrt输出平方根,将其四舍五入到小数点后三位round(,3)

感谢@caird coinheringaahing注意到先前的答案没有舍入。


是否“仅将c保留到小数点后三位”?
caird coinheringaahing

@cairdcoinheringaahing:现在可以了!谢谢 !
弗雷德里克

1

ARBLE,73个字节

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

在线尝试!


1
@ H.PWiz我必须说,对于应该是的挑战sqrt(a^2+b^2),这有很多不必要的样板。
ATaco

1

OML,57字节

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

在线尝试!

第1部分

这只是输出字符串

"The hypotenuse of this right triangle is "s

第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

果冻,32个字符

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

在线尝试!

可能有更好的字符串压缩,它使我可以绕开需要与空格连接的地方,但是我找不到它。

说明:

,²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.

只要only three decimal places手段less than or equal to three decimal places,输出看起来就很好。
工程师吐司
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.