大学新生作业,求解
判断一个数是否为完数。例如:6=3+2+1,6等于其因子之和,所以6是完数。请绘制出算法的传统流程图和N-S流程图
#include <stdio.h> int main(void) { int i; int number; int sum = 0; printf("请输入整数: "); scanf("%d", &number); for (i = 1; i < number; i++) { if (number % i == 0) { printf("因子: %d ", i); sum += i; } } printf("\n结果:"); if (sum == number) { printf("%d 是完数\n", number); } else { printf("%d 不是完数\n", number); } return 0; }
#include <math.h> #include <stdio.h> int prime(unsigned long long n) { unsigned long long i,k; k=sqrt(n)+1; for(i=2; i<=k; i++) { if(n%i == 0) return 0; } return 1; } unsigned long long powint(unsigned int base, unsigned int power) { unsigned int i; unsigned long long result = 1; for(i=0;i<power;i++) { result *= base; } return result; } int main() { unsigned long long mp=0; unsigned int pow=1, count=0; printf("Mersenne Prime:\n"); while(mp<0xffffffffffffffff) { pow++; mp=powint(2,pow)-1; if(prime(mp)) { count++; printf("M(%u)=%llu", pow-1, mp*powint(2,pow-1)); printf("\n"); } } printf("the number of Mersenne Prime less than unsigned long long is:%d\n", count); return 0; }