Python testing Nicholas theorem

CSDN Q & A 2022-05-15 04:51:40 阅读数:972

pythontestingnicholastheorem

Defined function Nicoqish(x) Return and for x The first and last values of consecutive odd numbers , A number may be written as multiple sets of continuous odd numbers and , Only the group of information with the smallest initial odd number is required to be returned . Input data in the main module n Before calling function output n Number dependent formula .
Input
One [1,20] Integer between n
Output
[1,n] Multiple line Expressions related to integers in the range
The sample input
5
Sample output
1^3=1
2^3=3+5
3^3=7+9+11
4^3=1+3+5+7+9+11+13+15
5^3=21+23+25+27+29

def Nicoqish(x):
k = x**3
i = 1
while i <= k:
s = 0
j = i # j>=i
while s<=k:
s += j
if s==k:
return i,j
j += 2
i+=2




Take the answer :

Hello, this is my solution 【 There may be other solutions 】

def Nicoqish(x): x=x**3 i=1 j=1 while sum(range(j,i+1,2))!=x: i+=2 if i>=x: i=1 j+=2 if j>=x: raise TimeoutError(" Numbers %d It cannot be written in the form of continuous odd addition ") return j,i

版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://primo.wiki/2022/131/202205041351465717.html