Examination of your attempt
Since you are trying to teach yourself C, let's actually show you what the issues are in your code and show you how to attack this problem properly utilizing the complete power of C.
#include <stdio.h>
The following declaration is useless
int primearray(int,int);
The declaration below should be ANSI compliant
main()
int main()
{
You should NOT use such a big array as a stack variable and actually either declare it
as a global variable or allocate it dynamically ( much better solution )
int alpha, k, j, i=1, prime[200000], count = 0;
prime[0]=2;
/* Define number which counts number of primes in interval [0,alpha] */
int totprime=1;
you should divide the code up into separate functions so that you can deal with
each piece simply and easily
For instance a function that takes a number and finds all primes using whatever
method, that way you can start out with a simple function and as you learn better
methods you can actually make that routine faster.
printf("Enter the number of which you want the prime factorization\n");
scanf("%d",&alpha);
/* This procedure makes a list of all prime numbers up to alpha */
The block below has many issues.
First it's suboptimal because it tests far too many numbers for primality.
The test for primality is incorrect and will not result in the value always equal to
j when they're supposed to be. Also it's very CPU intenstive you can use modulo operator
to get results more effectively
The tests for storing the primes is wierd.
This entire block should be in its own function.
for (j=3 ; j<alpha ; j++)
{
for (k=0 ; k<i ; k++)
{
if ((j/prime[k])*prime[k]==j)
break;
else
count = count + 1;
}
if (count == i)
{
prime[i]=j;
i = i + 1;
count = 0;
totprime = totprime + 1;
}
else
count = 0;
}
Don't comment obvious code
the code shows that you're going to print the primes
adding a comment about is frivilous and detracts
/* Print prime numbers */
printf("The primes are:\n");
for (j=0 ; j<totprime ; j++) printf("%d\n",prime[j]);
Using for loop the way you did above is poor form and you should
not use the printf statement on the same line.
Generally speaking: Your tabing and block style is inconsistent when you are
coding you should use a consistent way to mark your blocks code is read far more often than it's written using a consistent style helps follow the logic
printf("Total number of primes: %d\n",totprime);
Dynamic arrays are not supported in C. The code below is not standard.
if your platform/compiler supports it you will get inconsistent results
when you port the code.
int primefac[totprime];
The block below has many problems.
for (j=0 ; j<totprime ; j++)
{
This test will fail because you are assuming things about how
integer division works (and whether or not it results in a fraction)
Not to mentions that it's an extremely expensive operation
if ( (alpha/prime[j])*prime[j] == alpha)
{
primefac[count] = prime[j];
count = count + 1;
}
The else below is completely not required by any logic
else
continue;
}
printf("Primefactors are\n");
Same issue as above
for (j=0 ; j<count ; j++) printf("%d\n",primefac[j]);
This should line up with the main function
}
/* STILL EDITING */ The correct approach coming up.
Okay now let's figure out how approach this problem correctly.
Stating the problem simply
You want to find the largest prime factor of 600851475143.
That is to say you want to find maximum value of N
$$ \left. 600851475143 \equiv 0 \pmod{N} \: \middle|
\: {N \not \equiv 0} \pmod {p} \; \forall p \in \mathbb{Z}^+ \right. $$
In C the modulo operator (%) (actually it's the remainder operator) provides the necessary capability.
So how do we do this, first let's create a function that does that. We know that if $ a \times b \equiv N \; \forall {a, b} \in \mathbb{Z}^+ $ then either $ a \leq \sqrt{N} $ or $ b \leq \sqrt{N} $
So a simple (crude) algorithm would be to see if there is a prime number $p$ such that: $ \left. p \leq \sqrt{600851475143} \; \big| \; p \, \vert \, 600851475143 \right. $
A correct approach
I will not provide you with a solution but walk you through some basic concepts.
First things first, it's a good idea to split things up into smaller chunks and treat each as a function. If you define your approach step by step then a good rule of thumb is to create one function per step. If it's a complex step, with simpler substeps, then that function should be divided into multiple sub functions. You may not get it right the first time, that's okay, you will develop better judgement as you go along as to how things should get divided up.
Okay let's look at the problem step by step using our simple algorithm using a flowchart

Now essentially each box in the flowchart above is going to the statements of your main function:
/**
* name: primes_upto
* parameters:
* M: finds all primes until M and stores them in zero-terminated list
* list: the pointer to an array of ints
* if list is NULL then it will allocate enough memory
* to store the primes + 1
* if list is not NULL it will truncate insert a 0 at
* the index where value of prime is > M
*
* return value: returns number of primes on success or 0 on failure
*/
int primes_upto(int k, int **list);
int main() {
const int original_N = 600851475143;
int N, a, S;
int *L = NULL; /* list of primes we need to fill */
int *ap = NULL; /* a memory pointer to integers that will point to the next prime */
N = original_N;
do {
S = sqrt(N); /* we have to provide this */
primes_upto(S, &L); /* we have to provide this function */
/* we will make sure that the primes_upto function
injects a zero at the end of the list. Since zero is not a prime
we can use it as a marker to indicate the end of the list
and that way we don't have to worry about how long the list is. */
for (ap = L; ap && *ap != 0; ap++) {
int a = *ap;
if (N % a == 0) {
N = N / a;
break;
}
}
} while (ap && *ap != 0);
printf ("The largest number that divides %d is %u\n", original_N, N);
}
jloop you can count up by 2 since you only need to check odd numbers for primality. On the secondjloop, count backwards through the primes until you find one that dividesalphasince you only need the largest. – Godric Seer Apr 26 '14 at 16:34