# Programming in C language (8): Nicolas theorem

Time：2020-6-5

#### Example 8 nicochez’s theorem

Title Description

Nicolas’s theorem can be described as: the cube of any integer can be expressed as the sum of a series of continuous odd numbers. It should be noted that these odd numbers must be continuous, such as: 1, 3, 5, 7, 9.

For example, for integer 5, 5 * 5 * 5 = 125 = 21 + 23 + 25 + 27 + 29.

For the integer 6216 = 31 + 33 + 35 + 37 + 39 + 41,

It can also be expressed as 216 = 7 + 9 + 11 + 13 + 15 + 17 + 19 + 21 + 23 + 25 + 27 + 29.

Please write a program to verify the theorem.

Input format

An integer n (2 ≤ n ≤ 1000).

Output format

The cube of n is expressed as the sum of a series of continuous odd numbers. See the output example for the specific format. If there are multiple representations, you can output any one.

sample input

29

sample output

29*29*29=24389=813+815+817+819+821+823+825+827+829+831+833+835+837+839+841+843+845+847+849+851+853+855+857+859+861+863+865+867+869

##### (1) Programming idea 1.

First, calculate the cubic num of the input number n, and then start to accumulate sum from 1 (record with variable I). Each time j plus 2 guarantees that the next number is also odd. If the sum is greater than the cubic num, skip this cycle and carry out the next attempt (I = 3 or 5, 7,…) Start accumulating sum). When found, record the start position (I), end position (J) and output.

The program is written as a nested double loop. External cycle I controls the starting point of cumulative sum, internal cycle I, I + 2, I + 4 And.

##### (2) Source 1.

#include

int main()

{

int n,num,sum,i,j,k,flag;

while(1)

{

scanf(“%d”,&n);

if(n==0)  break;

num = n * n * n;

flag=0;

for(i=1; i

{

sum=0;

for(j=i; j

{

sum += j;

if(sum == num)

{

printf(“%d*%d*%d=%d=%d”,n,n,n,num,i);

for (k=i+2; k<=j;k+=2)

printf(“+%d”,k);

printf(“\n”);

flag=1;

break;

}

else if (sum > num)

break;

}

}

}

return 0;

}

##### (3) Programming idea 2.

The idea of source program 1 is to verify nicochez’s theorem by trial method and to realize it by double cycle.

In fact, the cube of N can be expressed as the sum of the items of an equal difference sequence. The first term of the equal difference sequence is n * N-N + 1, the tolerance is 2, and the number of items is n.

According to the summation formula of the sequence, the sum of the sequence is:

[(n*n-n+1)+( n*n-n+1)+ 2 (n-1)]*n/2 =n*n*n

Therefore, it is enough to output the items of this series of equal difference numbers directly by means of circulation.

##### (4) Source 2.

#include

int main()

{

int n,a,i;

while(1)

{

scanf(“%d”,&n);

if(n==0)  break;

//The first term is n * N-N + 1, the tolerance is 2, and the number of terms is n

a=n*n-n+1;

printf(“%d*%d*%d=%d=%d”,n,n,n,n*n*n,a);

for (i=1; i

printf(“+%d”,a+i*2);

printf(“\n”);

}

return 0;

#### Exercise 8

##### 8-1 guess of Valley angle

Title Description

When studying natural numbers, Japanese mathematician Takeo Tanaka found a strange phenomenon: for any natural number n, if n is an even number, divide it by 2; if n is an odd number, multiply it by 3, and then add 1. In this way, after finite operations, we can always get the natural number 1. People call this discovery of Gujiao Jingfu “Gujiao conjecture”.

Please write a program to verify this conjecture.

Input format

A natural number n.

Output format

After a finite number of operations, n is finally turned into the whole process output of natural number 1. See the output sample for the specific format.

sample input

34

sample output

34/2=17

17*3+1=52

52/2=26

26/2=13

13*3+1=40

40/2=20

20/2=10

10/2=5

5*3+1=16

16/2=8

8/2=4

4/2=2

2/2=1

(1) Programming ideas.

According to the valley angle conjecture, we can get two iterative relations: when n is even, n = n / 2; when n is odd, n = n * 3 + 1.

This is the iterative process that needs to be repeated by the computer. How many times does this iterative process need to be executed repeatedly to make the iterative variable n finally become a natural number 1, which we can’t calculate. Therefore, it is necessary to further determine the conditions for ending the iterative process. As for any given natural number n, only after a finite number of operations, the natural number 1 can be obtained, thus completing the verification work. Therefore, the condition used to end the iteration process can be defined as: n = = 1.

(2) Source program.

#include

int main()

{

unsigned int data;

scanf(“%d”,&data);

while(data!=1)

{

if((data%2==0))

{

printf(“%d/2=%d\n”,data,data/2);

data/=2;

}

else

{

printf(“%d*3+1=%d\n”,data,data*3+1);

data=data*3+1;

}

}

return 0;

}

##### 8-2 square theorem

Title Description

The famous “square theorem” in number theory is that all natural numbers can be represented by the sum of squares of four integers no less than 0 at most.

Write a program to verify the theorem.

Input format

A natural number n.

Output format

The natural number n is expressed as the sum of squares of four numbers. See the output sample for the specific format.

sample input

147

sample output

7*7+7*7+7*7+0*0=147

8*8+7*7+5*5+3*3=147

9*9+5*5+5*5+4*4=147

9*9+7*7+4*4+1*1=147

9*9+8*8+1*1+1*1=147

11*11+4*4+3*3+1*1=147

11*11+5*5+1*1+0*0=147

12*12+1*1+1*1+1*1=147

(1) Programming ideas.

For the natural number n to be verified, four variables I, J, K and L are used to calculate exhaustively, and the calculation results are output when the requirements (I * I + J * j + k * k + L * l = = n) are met.

In the case of exhaustion, I ≥ J ≥ K ≥ l may be assumed. Therefore, the scope of exhaustion can be determined as follows:

1 ≤ i ≤ n/2

0 ≤ j ≤ i

0 ≤ k ≤ j

0 ≤ l ≤ k

(2) Source program.

#include

int main()

{

int n,i,j,k,l;

scanf(“%d”,&n);

For (I = 1; I < = n / 2; I + +) / / enumerate I, J, K, l

for (j = 0; j <= i; j++)

for (k = 0; k <= j; k++)

for (l = 0; l <= k; l++)

if (i *i + j * j + k * k + l * l == n)

{

printf(“%d*%d+%d*%d+%d*%d+%d*%d=%d\n”,i,i,j,j,k,k,l,l,n);

}

return 0;

}

##### 8-3 kablek operation

Problem description

The so-called kablek operation refers to any four digit number, as long as the numbers in each digit are not the same, there is such a rule:

(1) Arrange the four numbers that make up the four digits from large to small to form the largest four digits that are made up of the four digits;

(2) Arrange the four numbers that make up the four digits from small to large to form the smallest four digits (if the four numbers contain 0, the number is less than four digits);

(3) Find the difference between the two numbers and get a new four digit number.

Repeat the above process, and the final result is 6174.

For example, n = 3280, the verification result is: 8320-238 = 8082 8820-288 = 8532 8532-2358 = 6174

Write a program to verify the operation of kablek.

input data

The numbers on one digit are not all the same four digit n.

Output requirements

After a finite number of kablek operations, n is finally transformed into a whole process output of 6174. See the output sample for the specific format.

sample input

2019

sample output

9210-129=9081

9810-189=9621

9621-1269=8352

8532-2358=6174

YES

(1) Programming ideas.

In order to implement the verification program, four functions are written.

void parse_ Sort (int each [], int Num) decomposes num into numbers, sorts them, and stores them in the array each [].

Int mind (int each []) finds the maximum number of four numbers in an array each.

Int maxd (int each []) finds the minimum number of 4 digits in the array each.

int pow10_ Int (int n) to the nth power of 10.

(2) Source program.

#include

#define N 4

int pow10_ Int (int n); / / find the nth power of 10

void parse_ Sort (int each [], int Num); / / decompose num into bits and store them in the array each []

Int mind (int each []); / / find the maximum number of array each

Int maxd (int each []); / / find the smallest number of each array

int main()

{

int number,max,min;

int each[N];

scanf(“%d”,&number);

while(number!=6174)

{

parse_sort(each,number);

max=maxD(each);

min=minD(each);

number=max-min;

printf(“%d-%d=%d\n”,max,min,number);

}

printf(” YES\n”);

return 0;

}

int pow10_ Int (int n) / / function to the nth power of 10

{

int sum=1;

for(int i=0;i

sum=sum*10;

return sum;

}

void parse_ Sort (int each [], int Num) / / decompose num into bits and store them in the array each []

{

int m,i,j,t;

for (i=0;i

each[i]=0;

i=0;

while(num!=0)

{

m=num%10;   num=num/10;

each[i++]=m;

}

for(i=0;i

for (j=0;j

if (each[j]>each[j+1])

{

t=each[j];

each[j]=each[j+1];

each[j+1]=t;

}

}

Int mind (int each []) / / find the maximum number of array each can be composed of

{

int sum=0,i;

for(i=0;i

sum+=each[i]*pow10_int( (N-1-i) );

return sum;

}

Int maxd (int each []) / / find the smallest number of each array

{

int sum=0,i;

for(i=0;i

sum=sum+each[i]*pow10_int(i);

return sum;

## The way of nonlinear optimization

Mathematical knowledge 1、 Nonlinear functionLinear function is another name of a function of first degree, then nonlinear function means that the function image is not a function of a straight line.Nonlinear functions include exponential function, power function, logarithmic function, polynomial function and so on. 2、 Taylor expansion1. Taylor formula:Taylor’s formula is to add a_ The […]