Lanqiao Cup-Algorithm Training-8-2 Find the Number

topic


Problem description
  If the sum of all factors smaller than itself of a natural number is equal to the number, it is called a complete number. Design an algorithm to print all the completed numbers between 1-9999.
Sample output
Insert picture description here


Ideas

The inner loop always judges whether it can be divided up, and accumulates if it can. After the inner loop, judge whether it is equal to i, and output i if it is equal.
I don’t know if there is a better solution for the final count. I only think of this at present.


Code

#include <stdio.h>
#include<math.h>
int main()
{
    
        
	int i,j;
	
	for(i=1;i<10000;i++){
    
    
		int num=0;
		for(j=1;j<i;j++){
    
    
			if(i%j==0){
    
    
				num+=j;
			}
		}
		if(num==i)
		printf("%d\n",i);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/mjh1667002013/article/details/114458003