使用maxima解决初等数论中的问题: 编辑
Elementary number theory using Maxima
Prime numbers
You might remember that for any integer greater than 1,
is a prime number if its factors are 1 and itself. The integers 2, 3, 5, and 7 are primes, but 9 is not prime because
. The command primep() is useful for testing whether or not an integer is prime:
1 2 3 4 5 6 7 8 9 10 |
|
And the command next_prime(n) returns the next prime number greater than or equal to :
1 2 3 4 5 6 7 8 9 10 |
|
Let’s now define a function called primes_first_n() in Maxima to return a list of the first primes, where
is a positive integer. Programming in the Maxima language is different from programming in other languages like C, C++, and Java. For example, if your variable is be assigned a number, you don’t need to define whether your variable is of type int, long, double, or bool. All you have to do is use the colon operator “:” to assign some value to a variable, like in the following example.
1 2 3 4 5 6 7 8 9 |
|
Before defining the function primes_first_n(), there are two useful built-in Maxima functions that you should know about. These are append() and last(). The function append() can be used to append an element to a list, whereas last() can be used to return the last element of a list:
1 2 3 4 5 6 7 8 |
|
Below is the function primes_first_n() which pulls together the features of next_prime(n),append(), and last(). Notice that it is defined at the Maxima command line interface.
1 2 3 4 5 6 7 8 9 10 11 |
|
You can also put the above function inside a text file called, say, /home/mvngu/primes.mac with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Like C++ and Java, Maxima also uses “/*” to denote the beginning of a comment block and “*/” to denote the end of a comment block. To load the content of the file/home/mvngu/primes.mac into Maxima, you use the command load(). Let’s load the above file and experiment with the custom-defined function primes_first_n():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Factorizing integers
Integer factorization is about breaking up an integer into smaller components. In number theory, these smaller components are usually the prime factors of the integer. Use the command ifactors() to compute the prime factors of positive integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The prime factors of 10 are and
. When you multiply these two prime factors together, you end up with
. The expression
is called the prime factorization of 10. Similarly, the expression
is the prime factorization of 25, and
is the prime factorization of 72.
Greatest common divisors
Closely related to integer factorization is the concept of greatest common divisor (GCD). The Maxima command gcd() is able to compute the GCD of two expressions and
where this makes sense. These two expressions may be integers, polynomials, or some objects for which it makes sense to compute their GCD. For the moment, let’s just work with integers:
1 2 3 4 5 6 |
|
The GCD of two integers and
can be recursively defined as follows:
where is the remainder when
is divided by
. The above recursive definition can be easily translated to a Maxima function for integer GCD as follows (credit goes to amca for the Maxima code):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Save the above code to a text file and load it first before using the function. Or you can define the function from the Maxima command line interface and proceed to use it:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The extended Euclidean algorithm provides an interesting relationship between , and the pair
and
. Here is a Maxima function definition courtesy of amca:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Or you can define it from the Maxima command line:
1 2 3 4 5 6 7 8 9 10 |
|
Let’s use the function igcdex() for various pairs of integers and verify the result.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
如果,您希望更容易地发现我的新文章,不妨点击一下绿色通道的【关注我】,亦可微信搜索公众号「大白技术控」关注我。
如果您觉得阅读本文对您有帮助,请点击一下右下方的推荐按钮,您的推荐将是我写作的最大动力!版权声明:本文为博主原创或转载文章,欢迎转载,但转载文章之后必须在文章页面明显位置注明出处,否则保留追究法律责任的权利。如您有任何疑问或者授权方面的协商,请 .
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?