I've been toying around with some Project Euler problems and naturally am running into a lot that require the handling of bigger than long long
type numbers. I am committed to using Cocoa and Objective-C (I need to stay sharp for work) but can't find an elegant way (read: library) to handle these really big numbers.
我一直在努力解决一些Project Euler问题,并且自然而然地遇到了很多需要处理大于long long类型数字的问题。我致力于使用Cocoa和Objective-C(我需要保持敏锐的工作),但找不到一种优雅的方式(阅读:库)来处理这些非常大的数字。
I'd love to use GMP but is sounds like using it with Xcode is a complete world of hurt.
我喜欢使用GMP,但听起来像使用它与Xcode是一个完整的伤害世界。
Does anyone know of any other options?
有没有人知道其他任何选择?
6 个解决方案
#1
If I were you I would compile gmp outside XCode and use just gmp.h and libgmp.a (or libgmp.dylib) in my XCode project.
如果我是你,我会在XCode之外编译gmp,并在我的XCode项目中使用gmp.h和libgmp.a(或libgmp.dylib)。
#2
Try storing the digits in arrays.
尝试将数字存储在数组中。
Although you will have to write some new functions for all your arithmatic problems but thats how we were told to do it in college.
虽然你必须为你所有的算术问题写一些新函数,但这就是我们被告知如何在大学里完成它。
Plus the speed of calculations was pretty improved as big numbers weren't really big afterall and were not numbers really altogether
加上计算的速度相当提高,因为大数字并不是真正的大数据,而且完全不是数字
see if it helps
看看它是否有帮助
regards
#3
vBigNum
in vecLib
implements 1024 bit integers (signed or unsigned). Is that big enough?
vecLib中的vBigNum实现1024位整数(有符号或无符号)。这够大吗?
#4
If you wanted to use matlab (or anything close) you could look at my implementation of a big integer form (vpi) on the file exchange.
如果你想使用matlab(或任何接近),你可以在文件交换上查看我的大整数形式(vpi)的实现。
It is rather simple. Store each digit separately. Adds and subtracts are simple, just implement a carry operation. Multiplies are best done using convolution, then a carry. Implement divide and mod operators, then a powermod operation, useful for many of the PE problems. Powers are easy - just repeated squaring and multiplication, based on the binary representation of the exponent.
这很简单。分别存储每个数字。添加和减法很简单,只需执行进位操作即可。乘法最好使用卷积,然后是进位。实现divide和mod运算符,然后执行powermod操作,对许多PE问题都有用。权力很容易 - 只需重复平方和乘法,基于指数的二进制表示。
This will let you solve many PE problems.
这将让您解决许多PE问题。
#5
I too got the bright idea to attempt some Euler Project problems with Cocoa/Objective-C and have found it frustrating. I previously used Java and perhaps some PHP. I posted my exact problem in this thread.
我也有明智的想法尝试使用Cocoa / Objective-C的一些Euler Project问题并且发现它令人沮丧。我之前使用的是Java,也许还有一些PHP。我在这个帖子中发布了我的确切问题。
#6
I always considered using a library cheating for this project. Just write a class with the things you need. And don't be afraid to use malloc and uint64_t and so on. NSNumber is not a good idea in many cases.
我一直考虑使用图书馆作弊这个项目。只需用你需要的东西写一堂课。并且不要害怕使用malloc和uint64_t等。在许多情况下,NSNumber不是一个好主意。
On the other hand, there are many problems where the obvious solution would require huge to enormously huge numbers, and the trick is to find a way to solve the problem without using these huge numbers. (For example, what is the sum of the last thousand digits of 1,000,000 factorial)?
另一方面,存在许多问题,其中明显的解决方案需要巨大到极大的数量,并且诀窍是找到一种方法来解决问题而不使用这些巨大的数字。 (例如,1,000,000阶乘的最后一千位数的总和是多少)?
#1
If I were you I would compile gmp outside XCode and use just gmp.h and libgmp.a (or libgmp.dylib) in my XCode project.
如果我是你,我会在XCode之外编译gmp,并在我的XCode项目中使用gmp.h和libgmp.a(或libgmp.dylib)。
#2
Try storing the digits in arrays.
尝试将数字存储在数组中。
Although you will have to write some new functions for all your arithmatic problems but thats how we were told to do it in college.
虽然你必须为你所有的算术问题写一些新函数,但这就是我们被告知如何在大学里完成它。
Plus the speed of calculations was pretty improved as big numbers weren't really big afterall and were not numbers really altogether
加上计算的速度相当提高,因为大数字并不是真正的大数据,而且完全不是数字
see if it helps
看看它是否有帮助
regards
#3
vBigNum
in vecLib
implements 1024 bit integers (signed or unsigned). Is that big enough?
vecLib中的vBigNum实现1024位整数(有符号或无符号)。这够大吗?
#4
If you wanted to use matlab (or anything close) you could look at my implementation of a big integer form (vpi) on the file exchange.
如果你想使用matlab(或任何接近),你可以在文件交换上查看我的大整数形式(vpi)的实现。
It is rather simple. Store each digit separately. Adds and subtracts are simple, just implement a carry operation. Multiplies are best done using convolution, then a carry. Implement divide and mod operators, then a powermod operation, useful for many of the PE problems. Powers are easy - just repeated squaring and multiplication, based on the binary representation of the exponent.
这很简单。分别存储每个数字。添加和减法很简单,只需执行进位操作即可。乘法最好使用卷积,然后是进位。实现divide和mod运算符,然后执行powermod操作,对许多PE问题都有用。权力很容易 - 只需重复平方和乘法,基于指数的二进制表示。
This will let you solve many PE problems.
这将让您解决许多PE问题。
#5
I too got the bright idea to attempt some Euler Project problems with Cocoa/Objective-C and have found it frustrating. I previously used Java and perhaps some PHP. I posted my exact problem in this thread.
我也有明智的想法尝试使用Cocoa / Objective-C的一些Euler Project问题并且发现它令人沮丧。我之前使用的是Java,也许还有一些PHP。我在这个帖子中发布了我的确切问题。
#6
I always considered using a library cheating for this project. Just write a class with the things you need. And don't be afraid to use malloc and uint64_t and so on. NSNumber is not a good idea in many cases.
我一直考虑使用图书馆作弊这个项目。只需用你需要的东西写一堂课。并且不要害怕使用malloc和uint64_t等。在许多情况下,NSNumber不是一个好主意。
On the other hand, there are many problems where the obvious solution would require huge to enormously huge numbers, and the trick is to find a way to solve the problem without using these huge numbers. (For example, what is the sum of the last thousand digits of 1,000,000 factorial)?
另一方面,存在许多问题,其中明显的解决方案需要巨大到极大的数量,并且诀窍是找到一种方法来解决问题而不使用这些巨大的数字。 (例如,1,000,000阶乘的最后一千位数的总和是多少)?