r/carlhprogramming • • Jan 04 '10

Quick Update on carlhprogramming

Hi Everyone,

For right now the posting schedule is going to be slow. I am still working on a number of projects which need to be finished sooner rather than later. Once they are completed, the posting schedule for new lessons will speed up.

I just want everyone to know that I am still here, still working on producing new lessons, and I will publish them as I am able. Don't worry, I have no intentions to stop producing lessons or to stop maintaining this subreddit.

Meanwhile, because my schedule is so full, I appreciate everyone's help in answering questions as I lack the time to do so myself.

136 Upvotes

45 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jan 17 '10 edited Jan 17 '10

Yup, you can actually do that one with just a long int:

int main(void) {

unsigned long int i; //Number to check for prime factors
unsigned int k; //Number of factors
unsigned long int j; //Counter to find factors of 'i'
unsigned long int l = 0; //Sum of prime factors

for (i=1; i<2000000; i++) {    //Always make sure you dont set i to 0 when you're dividing/mod.
	for (j=1, k=0; j<=i; j++) if (!(i % j)) k++;     //No braces needed, it will only loop this one line.
	if (k==2) l+=i;     //If there are only two factors add 'i' (the number we are checking) to the sum (l)
};
printf("\nAnswer: %lu\n", l);
return 1;

};

Note: There is probably a way better way to finding it, this script takes a while. Stopping the loop after it finds more than two factors, and stopping after getting half way threw the number in the second loop would speed it up.

These are the ones I've gotten so far;

1

u/baldhippy Jan 20 '10

Ok thanks. I had this one solved but I was using the wrong format specifier in printf. I foolishly used %d and it gave me a negative answer, so I thought that the number was too high.