C language classic programming 282 examples 05 (Fibonacci sequence, Goldbach conjecture, Nicoche theorem)

027 Fibonacci Sequence

Fibonacci (the Fibonacci) Sequence characteristics: the first one and the second number is 1, the third starting number, this number is the sum of the first two and, before seeking the number of columns of the element 30,
Insert picture description here
in Mathematically, the Fibonacci sequence is defined in a recursive method: Insert picture description here
treat the subscript of F as an array subscript

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>



  main()
{
    
    
    long  f[100];
    int i;
    f[0] = 0;
    f[1] = 1;
    f[2] = 1;
    
    for(i = 3; i <= 30; i++)		//从第3项开始,每项等于前2项之和
    {
    
    
    	f[ i ] = f[i - 1] + f[i - 2];
	}
	
	for(i = 0;i <= 30; i++)
	{
    
    
			printf("%10ld", f[i]);
			if(i % 5 == 0)			//每5个元素进行一次换行
			{
    
    
				printf("\n");
			}
	}
 } 

Insert picture description here

028 Goldbach Conjecture

Verify that positive even numbers within 100 can be decomposed into the sum of 2 prime numbers, that is, verify that Goldbach’s conjecture holds for positive even numbers within 100 (greater than 2).
Such as:Insert picture description here

The method of expressing an even number as the sum of two prime numbers is equal to the number of intersections of blue and red on the same horizontal line.The method of expressing an even number as the sum of 2 prime numbers is equal to the blue and red lines on the same horizontal line

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>


int ss(int i)
{
    
    
	int j;
	
	if(i <= 1)			//小于1的数不是素数
	{
    
    
		return 0;
	}
	if(i == 2)			//2是素数
	{
    
    
		return 1;
	}
	
	for(j = 2; j < i; j++)		//对大于2的数进行判断,素数:除了一和他本身
	{
    
    
		if(i % j == 0)
		{
    
    
			return 0;
		}
		else if(i != j + 1)
		{
    
    
			continue;
		}
		else
		{
    
    
			return 1;
		}
	}
}

  main()
{
    
    
	int i, j, k, flag1, flag2, n = 0;
	
	for(i = 4; i < 100; i += 2)
	{
    
    
		for(k = 2; k <= i / 2; k++)
		{
    
    
			j = i - k;
			flag1 = ss(k);        		//判断拆分出的数是否是素数
			
			if(flag1)
			{
    
    
				flag2 = ss(j);
				if(flag2)				//是素数拆分
				{
    
    
					printf("%3d = %3d + %3d,", i, k, j);
					n++;
					
					if(n % 5 == 0)
					{
    
    
						printf("\n");
					}
				}
			 } 
		}
	 } 
	
     printf("\n");
 } 
  • To decompose a positive and even number into 2 parts, then judge the two parts, if they are both prime numbers, then the meaning of the question is met, and if they are not, then judge again

029 Nicoche's Theorem

Nicoche's theorem: Any cube of an integer can be written as a series of continuous odd sums. Use programming to verify the theorem. For example, enter 5 and run the result:

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>

  main()
{
    
    

	int i, j, k = 0, l, n, m, sum, flag = 1;
	
	printf("请输入一个数:\n");
	scanf("%d", &n);
	
	m = n * n * n;					//计算输入值的立方
	i = m / 2 ;						//立方值得一半
	if(i % 2 == 0)					//为偶数加一
	{
    
    
		i = i + 1;
	}
	
	while(flag == 1 && i >=1)		//当i大于等于1且flag=1时执行该语句
	{
    
    
		sum = 0;
		k = 0;
		while(1)
		{
    
    
			sum += (i - 2 * k);		//奇数累加求和
			k++;					//输出值判断
			if(sum == m)			//sum 和m相等,输出累加过程
			{
    
    
				printf("%d * %d * %d = ", n, n, n, m);
				for(l = 0; l < k-1; l++)
				{
    
    
					printf("%d + ", i - l *2);
				}
				printf("%d\n", i - (k - 1) * 2);
				flag = 0;
				break;
			 } 
			 
			 if(sum > m)			//超过退出重新计算
			 {
    
    
			 	break;
			 }
		}
		
		i -= 2;						//使初始值减小
	}
	
	
     printf("\n");
 } 
  • First determine the maximum value of this string of consecutive odd numbers, which can be analyzed like this, any cube value (sum) is half (x), if it is an odd number, then the value of x + x + 2 must be greater than sum, then the maximum of this string of consecutive odd numbers The value will not exceed x; if x is an even number, you need to turn it into an odd number, then it becomes an odd number plus 1, because x + 1 + x-1 is exactly equal to sum, so when x is an even number, the string is continuous The maximum value of odd numbers will not exceed x + 1, after determining the range, you can start exhaustively from the maximum value.

Reference:
Goldbach's conjecture: https://blog.csdn.net/bjweimengshu/article/details/103776026

Guess you like

Origin blog.csdn.net/qq_41070511/article/details/110287018