末三位数

csdn上的一道编程练习题,我觉得称为(3 + √5)^n问题更贴切些

与其说这是一道编程题,不如说这是一道数论推导题。

好了,言归正装,下面开始题解

题意:让求(3 + √5)^n 的末三位数是多少,输出即可

首先令 an=(3 + √5)^n + (3 - √5)^n  这个an是一个整数,也就是一个整数可以用两个无理数表示,有人问了,为什么an是一个整数呢?

其实证明很简单,只需用(3 + √5) ^n和 (3 - √5)^n到高中所学的二次项分解即可,将(3 + √5)^n 和 (3 - √5)^n 分别进行二次项分解然后相加就会发现,无理数的次方全是奇数,二者正好抵消,剩下的全是整数的项。

如果我们能求出an , 期中(3 - √5)^n 是一个小于1的数,(3 + √5)^n=an-(3 - √5)^n 所以,(3 + √5)^n 的末三位数也就是an-1 的末三位数

所以求出an就可以了,怎么去求an呢?

想到斐波那契数列 an=a(n-1)+a(n-2)  在这我们求an也利用这个思想,令 an=p*a(n-1)+q*a(n-2)  下面的问题就是去求参数p和q的值 

再者,(3 + √5) 和 (3 - √5) 是 某个二次方程的两个根,利用求根公式可以得到这个二次方程为  x^2-6*x+4=0  变换方程得 x^2=6*x-4  

两个方程的形式很像,所以可以推到出 p=6 q=-4;

知道了an=6*a(n-1)-4*a(n-2) 期中a(0)=2 a(1)=6 a(2)=28其他的数字就完全可以求出来了。

细心的人如果打印出这个an数列的后三位,其实它是个循环往复的,找到这个规律的话,会使复杂度降的更低。

代码我就不贴了,大家自己写喽

参考博客:http://blog.youkuaiyun.com/dahlwuyn/article/details/28647131

在 C 语言中,如果你想表示一个数字的幂运算的三位数,可以使用取模运算 `%` 结合循环来实现。假设你想计算 `base` 的 `exponent` 次方的三位,你可以这样做: ```c #include <stdio.h> #include <string.h> // Function to calculate the last three digits of a power char* last_three_digits(int base, int exponent) { char result[4] = {&#39;0&#39;, &#39;0&#39;, &#39;0&#39;, &#39;\0&#39;}; while (exponent > 0) { if (exponent % 100 >= 10) { // If there are more than one digit in the current position int temp = (base * (exponent % 10)) % 1000; // Calculate the next digit by multiplying and taking mod 1000 result[0] = temp % 10 + &#39;0&#39;; // Store the first digit result[1] = (temp / 10) % 10 + &#39;0&#39;; // Store the second digit result[2] = temp / 100; // Store the third digit as an integer, no need for modulus here } else { // If it&#39;s just one or zero digits, multiply once and store directly int temp = base * (exponent % 10); result[0] = temp % 10 + &#39;0&#39;; exponent -= 10; } exponent /= 10; // Reduce the exponent for the next iteration } return result; } int main() { int base, exponent; printf("Enter base and exponent: "); scanf("%d%d", &base, &exponent); char* result = last_three_digits(base, exponent); printf("Last three digits of %d^%d are: %s\n", base, exponent, result); free(result); // Don&#39;t forget to free the dynamically allocated memory return 0; } ``` 这个函数首先初始化一个字符数组 `result` 来存储结果,并通过循环逐步计算并更新每个位置的值,直到 `exponent` 变为零。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值