1. Each non prime number (sum) can be written in the form of multiplication of several prime numbers (also known as prime numbers), which are called the prime factors of the sum. For example, 6 can be decomposed into 2x3, and 24 can be decomposed into 2x2x3. Now, your program will read an integer in the range of [2100000], and then output its prime factorization; when
When you read a prime number, output itself.
There is no space between all the symbols. x is the lowercase letter x.
#include <stdio.h> int prime(int a); //Judge whether a is prime int main() { int num,c,ins,mask; scanf("%d",&num); c=prime(num); if (c==1){ printf("%d=%d\n",num,num); }else{ printf("%d=",num); do{ ins=1; do{ ins++; mask=0; if (prime(ins)==1){ //If ins is prime, then calculate mask mask=num%ins; } }while(mask!=0||prime(ins)==0); //When num cannot divide ins by integers or ins is not a prime number, the internal loop, i.e. ins, is executed++ num=num/ins; printf("%dx",ins); }while(prime(num)==0); //When the calculated num is not a prime number, the external loop, i.e. ins initialization, is executed printf("%d\n",num); } return 0; } int prime(int a) //Judge whether a is prime { int i; int isprime=1; for (i=2;i<a;i++){ if (a%i==0){ isprime=0; } } return isprime; }
2. The factor of a positive integer is all the positive integers that can divide it. If a number is exactly equal to the sum of factors except its own body, it is called a perfect number. For example, 6 = 1 + 2 + 3 (the factor of 6 is 1, 2, 3). Now, you write a program that reads in two positive integers n and m (1 < = n < m < 1000) and outputs all the completions in the [n,m] range.
#include <stdio.h> int perfect(int a); //Judge complete number int main() { int count,n,m,i; count=0; scanf("%d %d",&n,&m); for(i=n;i>=n&&i<=m;i++){ if (perfect(i)==1){ count++; //Calculate the number of complete numbers within the set range } } for(i=n;i>=n&&i<=m;i++){ if (perfect(i)==1){ printf("%d",i); count--; if (count!=0){ //When the number is reduced to 0, it means the last complete number within the specified range printf(" "); } } } printf("\n"); return 0; } int perfect(int a) //Judge complete number { int ins,mask; int sum=0; int isperfect=0; for(ins=1;ins<=a-1;ins++){ mask=a%ins; if (mask==0){ sum=sum+ins; } } if (sum==a){ isperfect=1; } return isperfect; }