在计划球拍中乘以大整数

发布于 2024-12-19 13:10:20 字数 256 浏览 5 评论 0原文

我在方案中实现了一个“big-int”作为列表,因此第一个元素是数字的符号(+或-),下面是数字本身的值,首先是个位,然后是十位等例如

(+ 0 0 1) 表示 100(- 9 2 3 1) 表示 -1329< /code> 等等

我现在需要什么就是实现以这种方式实现的大整型的加法、减法和乘法。我已经完成了加法和减法,请问有人可以帮我做乘法吗?

I've implemented a "big-int" in scheme as a list, so the first element is the sign of the number (+ or -) and the following are the value of the number itself, first the ones, then the tens etc.

For example: (+ 0 0 1) is for 100, (- 9 2 3 1) is for -1329 etc.

What I need now is to implement addition, subtraction and multiplication for big-ints implemented in this manner. I've done addition and subtraction, can someone help me with multiplication, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心小果奶 2024-12-26 13:10:20

将问题分解成更小的部分。首先,编写一个将 Big-int 乘以一位数的函数。然后,将其扩展为一个将 Big-int 乘以数字列表的函数(使用 Greg Hewgill 的提示,您可能知道如何在纸上执行此操作)。最后,将其包装在一个接受两个 Big-int 的函数中,去掉符号,然后调用之前的函数。

我强烈建议您在开发这些功能之前为它们编写测试用例。

Break the problem up into smaller pieces. First, write a function that multiplies a Big-int by a single digit. Then, extend this (using Greg Hewgill's hint that you probably know how to do this on paper) to a function that multiplies a Big-int by a list of digits. Finally, wrap this in a function that accepts two Big-ints, strips off the sign, and then calls your previous function.

I strongly suggest that you write test cases for these functions before developing them.

十六岁半 2024-12-26 13:10:20

这是一种众所周知的大整型乘法的分而治之方法:

http://ozark.hendrix.edu/~burch/csbsju/cs/160/notes/31/1.html

这种方法将两个数字切割成一半并递归地处理它们。它非常快,并且很容易实现,尽管它的解释看起来很长。我强烈推荐它。它需要用其他语言编写大约 10 行代码,在方案中可能更少:)。

here is a well known divide-and-conquer approach to big-int multiplication:

http://ozark.hendrix.edu/~burch/csbsju/cs/160/notes/31/1.html

This approach cuts the two numbers into halves and deals with them recursively. It is very fast, and it is easy to implement, despite the long explanation it may seem like. I highly recommend it. It takes all of about 10 lines of code in other languages, probably fewer in scheme :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文