Find greatly looking in the wrong ~ ~

Category: C/C++ -> C Author: cdj8806 Date: 2009-10-14 18:33:07
 
cdj8806
2009-10-14 18:33:07
Topic: If a number is exactly equal to the sum of its factors, this number is called a "complete count." For example, a factor of 2, 3, 6, and 6 = 1 +2 +3, so 6 is "End counts." Programmed to identify all finished within 1000 the number of output factors according to the following format: 6 its factors are 1,2,3
program I wrote as follows:
# include <stdio . h>
# include <math.h>
void main ()
{
int i, j, k, now1, now2, sum = 1;
printf ("1 its factors are 1 \ n");
for (i = 2; i <= 1000; i + +)
{
now1 = now2 = i;
k = sqrt (i);
for (j = 2 ; j <= k; j + +)
{
while (now1% j == 0)
{
; now1 = now1% j;
sum = sum + j;
}
}
if (now1 ! = 1) sum = sum + now1;
if (sum == now2)
{
printf ("% d ; its factors are 1 ", now2);
for (j = 2; j <= k; j + +)
{
; while (now2% j == 0)
{
printf (",% d", j);
now2 = now2 % j;
}
if (now2! = 1) printf (",% d", i);
}
printf ("\ n");
}
sum = 1;
}
} Which greatly

give correct ah, looked half is not apparent Wrong Le.
lbk2029612lbk
2009-10-14 18:51:09
Reference
#include <stdio.h>

int main(void)
{
int m,s,i;

for(m=2;m<1000;m++)
{
s = 0;
for(i = 1;i < m;i++)
if((m % i) == 0)   s = s + i;
if(s == m)
{
printf("%d,its factors are",m);
for(i = 1;i < m;i++)
if(m % i == 0)  printf("%d,",i);
printf("\n");
}
}
return 0;
}
pzt22843
2009-10-14 18:53:51
for (j = 2; j <= k; j + +)
{
while (now1% j == 0)
{
now1 = now1% j; / / this should be now1 = now1 / j;
sum = sum + j;
}
s359374901
2009-10-14 19:04:54
amount, it seems that I put out the concept of factor wrong. . .
cfy_schao
2009-10-14 19:20:08
Alas, the original code for the / and% confused. . Poor me. . Thank you, above two big Le, I knot posted Kazakhstan
a19861229
2009-10-14 19:39:13
come late, give a little code of reference.

#include <stdio.h>

#define MAX_NUM 100000

int main() {
int num, idx, total;
for (num = MAX_NUM; num > 0; --num) {
total = 0;
for (idx = 1; idx < num; ++idx) {
total += num % idx == 0? idx : 0;
if (total > num) {
break;
}
}
if (total == num) {
printf("%d\n", num);
}
}

getchar();
return 0;
}
lv715789
2009-10-14 19:51:49
Upstairs not required to do, oh, if only so that the output is the same. .
hanyang0418
2009-10-14 19:56:11
but thank you for your help Kazakhstan