C language case perfect square number-07

Question: An integer, adding 100 to it is a perfect square number, and adding 168 to it is a perfect square number, what is the number?

Step 1: Define Program Goals

Write a C program to calculate an integer. After adding 100 to it, it becomes a perfect square number. Adding 168 makes it a perfect square number. What is the number?

Step 2: Program Design

The principle of perfect square numbers: If a positive integer a is the square of another positive integer b, then this a is called a perfect square number.
Program design:
1. Analysis process:
Set an integer as x, then according to the title, you can get two formulas:
(1) x+100=m^2
(2) x+100+168=n^2
(3)( m+n)(mn)=168 //Get 3 formulas from formulas 1 and 2
(4) Let i=m+n, j=mn, then i j=168, i, j have at least one even number here
(5 ) m=(i+j)/2, n=(ij)/2, here we can get that either ij is even or odd (6
) From 4 and 5, i and j are greater than etc. The even number of 2
(7) is i
j=168, j>=2, so 1<i<168/2+1;
at this point, we can calculate the data by traversing i
2. Design process:
module One: Use a for function to traverse and find all i values.
Module 2: Judging that the value of modulo i of 168 is 0
Module 3: Use if to judge, and output the corresponding value

code writing

#include<stdio.h>
int main(){
    
    
    int i,j,m,n,x;
    for(i=1;i<168/2-1;i++){
    
    
        if(168%i==0){
    
    
            j=168/i;
            if(i>j && (i+j)%2==0 && (i-j)%2==0){
    
      //判定条件是i>j,且(m+n)与(m-n)都是大于2的整数
                m=(i+j)/2;
                n=(i-j)/2;
                x=n*n-100;
                //输出输出符合条件的完整过程
                printf("%3d+100=%3dx%3d\n",x,n,n);
                printf("%3d+268=%3d*%3d\n",x,m,m);
                printf("这个数为:%d\n",x);  //输出符合条件的整数
            }
        }
    }

    return 0;
}

Summarize

This kind of programming topic is actually a math topic. It focuses on everyone's understanding of mathematical logic. Maybe people who are new to programming don't understand how learning programming is related to mathematics? In fact, programming is implemented using mathematical knowledge, and people who are good at mathematics generally do better in program implementation. Of course, there is no need to worry about whether bad mathematics will affect the writing of programs, because it is not common to use very advanced mathematical knowledge to write programs in life or work, and a general knowledge of mathematics is enough. Well, see you in the next chapter, come on!

Guess you like

Origin blog.csdn.net/weixin_37171673/article/details/132164796