首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GCD / LCM聚合体!

GCD / LCM聚合体!
EN

Code Golf用户
提问于 2017-04-17 18:35:40
回答 4查看 2.5K关注 0票数 28

您的挑战是制作一个程序或函数,以一种语言输出其输入的GCD,在另一种语言中输出其输入的LCM。GCD或LCM的内置程序(我正在看您,Mathematica)是允许的,但不鼓励。将有两个输入,它总是正整数,永远不会大于1000。

测试用例

每一行都是x y => GCD(x,y) LCM(x,y)格式的一个测试用例:

代码语言:javascript
复制
1 1 => 1 1
1 2 => 1 2
4 1 => 1 4
3 4 => 1 12
7 5 => 1 35
18 15 => 3 90
23 23 => 23 23
999 1000 => 1 999000
1000 999 => 1 999000
1000 1000 => 1000 1000

看这个巴斯泰尔为所有可能的输入与0 < x, y < 31。请注意,相同语言的不同版本作为不同的语言计算。

EN

回答 4

Code Golf用户

回答已采纳

发布于 2017-04-18 03:27:21

果冻 / 实际上,2字节

代码语言:javascript
复制
00000000: 1e 67                                            .g

这是提交程序的十六进制(xxd)。它不能在线测试,因为TIO不支持CP437编码。@Mego很好地验证了这一点是否适用于Cygwin,Cygwin实际上实现了CP437。

果冻: GCD

杰利使用果冻代码页,因此它可以看到以下字符。

代码语言:javascript
复制
œg

在网上试试!

是如何工作的

œ是一个不完整的令牌,因此被忽略。g是GCD内置的.

实际: LCM

实际上使用CP 437,因此它可以看到以下字符。

代码语言:javascript
复制
▲g

在网上试试!

是如何工作的

是LCM输入。因为g (GCD)需要两个整数输入,所以不执行它。

票数 23
EN

Code Golf用户

发布于 2017-04-17 19:05:35

爱丽丝果冻,9字节

Alice计算LCM:

代码语言:javascript
复制
//L
oi@g

在网上试试!

看起来像空格的是0x7FDEL控制字符。

果冻计算GCD。由于Jelly使用了只与可打印的ASCII兼容的自己的代码页,因此linefeed和DEL字符分别转换为½和linefeed:

代码语言:javascript
复制
//L½oi@
g

在网上试试!

Explanations

果冻是微不足道的:第一行定义了一个无意义的辅助链接,第二行是实际的程序,它只是包含GCD内建的。

Alice有点棘手,但它也使用了内置的:

代码语言:javascript
复制
/   Reflect to SE. Switch to Ordinal.
    While in Ordinal mode, the IP bounces diagonally up and down through the grid.
i   Read all input as a single string.
L   Compute the shortest common superstring of an empty string and the input. That
    is simply the input itself, so this does nothing.
    After two more bounces, the IP hits the top right corner and turns
    around, continuing to bounce up and down while moving west.
L   Still does nothing.
i   Try to read more input, but this simply pushes an empty string.
/   Reflect to W. Switch to Cardinal.
    The IP wraps to the last column.
L   Implicitly discard the empty string and convert the input to two integers.
    Compute their LCM.
/   Reflect to NW. Switch to Ordinal.
    The IP immediately reflects off the top boundary to move SW instead.
o   Implicitly convert the LCM to a string and print it.
    Reflect off the bottom left corner and move back NE.
/   Reflect to S. Switch to Cardinal.
i   Try to read a byte, but we're at EOF, so this pushes -1 instead. Irrelevant.
    The IP wraps back to the first line.
/   Reflect to NE. Switch to Ordinal.
    The IP immediately reflects off the top boundary to move SE instead.
@   Terminate the program.
票数 8
EN

Code Golf用户

发布于 2017-04-17 19:12:21

JS (ES6),高尔夫球语言,31字节(非竞争)

在这一挑战之后,增加了CGL的LCM特性。

代码语言:javascript
复制
 g=(a,b)=>b?g(b,a%b):a
//-LⓍ

看起来像是一个空间,实际上是一个不间断的空间,是CGL的一个注释。JS计算GCD:

代码语言:javascript
复制
g=(a,b)=>b?g(b,a%b):a

CGL计算LCM:

代码语言:javascript
复制
//  does nothing
- decrements the current stack number, resulting in it pointing to input
L computes the LCM of the first and second stack items and pushes it to the stack
Ⓧ prints out the last stack item

试试看:

代码语言:javascript
复制
Snippetify( g=(a,b)=>b?g(b,a%b):a
//-LⓍ
);
代码语言:javascript
复制
<script src="https://programmer5000.com/snippetify.min.js"></script>
<input type = "number">
<input type = "number">
票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/116809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档