
一个平方金字塔数是指自然数的平方之和。自然数包括从1到无穷大的所有数字。例如,前4个平方金字塔数分别为1、5、14、30。
为了更好地理解,考虑以下事实:如果我们以一开始的平方金字塔数为基础,将数字球堆叠在降序中,它们会形成一个金字塔。
问题陈述
给定一个数Sum。如果Sum是前n个自然数的平方和,返回n,否则返回false。
Example 1
的翻译为:
示例 1
Input = 30Output = 4
Explanation = 30是前4个自然数的平方和。
1*1 + 2*2 + 3*3 +4*4 = 30.
因此,输出应该是4。
Example 2
Input = 54Output = -1
Explanation = 没有任何n个自然数的平方和等于54。因此,输出应该是-1。
问题陈述的解决方案
这个问题有两个解决方案。
方法一:暴力解法
暴力破解方法是从n = 1开始。创建一个变量’total’,将下一个自然数的平方加到total的前一个值上。如果total等于Sum,则返回n,否则,如果total大于Sum,则返回false。
伪代码
startn =1 While (total < sum ): Total += n*n n=n+1if(total == sum) : Return nElse: Return falseend
Example
下面是一个C++程序,用于检查给定的数字是否是自然数的平方数之和。
#include using namespace std;// This function returns n if the sum of squares of first // n natural numbers is equal to the given sum// Else it returns -1int square_pyramidal_number(int sum) { // initialize total int total = 0; // Return -1 if Sum is <= 0 if(sum <= 0){ return -1; } // Adding squares of the numbers starting from 1 int n = 0; while ( total < sum){ n++; total += n * n; } // If total becomes equal to sum return n if (total == sum) return n; return -1;}int main(){ int S = 30; int n = square_pyramidal_number(S); cout<< "Number of Square Pyramidal Numbers whose sum is 30: "<< endl; (n) ? cout << n : cout << "-1"; return 0;}
输出
Number of Square Pyramidal Numbers whose sum is 30: 4
时间复杂度 – O(sum),其中sum是给定的输入。
空间复杂度 – O(1):没有使用额外的空间。
使用牛顿拉弗森方法的方法2
另一种方法是牛顿拉夫逊法。 牛顿拉夫逊法 用于找到给定函数 f(x) 的根和一个根的初始猜测。
sum of squares of first n natural numbers = n * (n + 1) * (2*n + 1) / 6, n * (n + 1) * (2*n + 1) / 6 = sum or k * (k + 1) * (2*k + 1) – 6*sum = 0
所以n是这个三次方程的根,可以使用牛顿-拉弗森方法来计算,该方法涉及从初始猜测值x0开始,使用下面的公式来找到下一个值x,即从先前的值xn得到的xn+1。
$$mathrm{x_{1}=x_{0}-frac{f(x_{0})}{f^{‘}(x_{0})}}$$
伪代码
Startcalculate func(x) and derivativeFunction(x) for given initial xCompute h: h = func(x) / derivFunc(x)While h is greater than allowed error ε h = func(x) / derivFunc(x) x = x – hIf (x is an integer): return xElse: return -1;end
Example
下面是一个C++程序,用于检查一个给定的数字是否是自然数的平方和。
#include#define EPSILON 0.001using namespace std;// According to Newton Raphson Method The function is// k * (k + 1) * (2*k + 1) – 6*sum or 2*k*k*k + 3*k*k + k - 6*sum double func(double k, int sum){ return 2*k*k*k + 3*k*k + k - 6*sum;}// Derivative of the above function is 6*k*k + 6*k + 1double derivativeFunction(double k){ return 6*k*k + 6*k + 1;}// Function to check if double is an integer or notbool isInteger(double N){ int X = N; double temp2 = N - X; if (temp2*10 >=1 ) { return false; } return true;}// Function that finds the root of k * (k + 1) * (2*k + 1) – 6*sumint newtonRaphson(double k, int sum){ double h = func(k, sum) / derivativeFunction(k); while (abs(h) >= EPSILON){ h = func(k, sum)/derivativeFunction(k); // k(i+1) = k(i) - f(k) / f'(k) k = k - h; } if (isInteger(k)) { return (int)k; } else { return -1; }}// Driver programint main(){ double x0 = 1; // Initial values assumed int sum = 91; int n = newtonRaphson(x0,sum); cout<< "Number of Square Pyramidal Numbers whose sum is 91: "<< endl; (n) ? cout << n : cout << "-1"; return 0;}
输出
Number of Square Pyramidal Numbers whose sum is 91: 6
Time Complexity – O((log n) F(n)) where F(n) is the cost of calculating f(x)/f'(x), with n-digit precision.
空间复杂度 – O(1):没有使用额外的空间。
结论
本文解决了找到给定和的平方金字塔数的问题。我们介绍了两种方法:一种是蛮力方法,另一种是高效方法。这两种方法都提供了C++程序。
以上就是平方金字塔数(平方和)的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444370.html
微信扫一扫
支付宝扫一扫