# Prime Factors

Page 1 of 1

## 1 Replies - 1497 Views - Last Post: 13 November 2010 - 11:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=364168&amp;s=d7d5a905f5d6e001c54b3001d54ab62d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

# Prime Factors

Posted 09 August 2006 - 07:09 AM

Description: Compile, build and runThis program returns da prime factors of any number u enter(Its kinda long. I wrote it while learning recursions).
```/*Louisda16th/Ashwith*/
/*Program To Generate The Prime Factors Of An Entered Numbers*/

#include

/*Function Prototypes*/
void factorcheck(long unsigned int prime,long unsigned int num );
void primefind(long unsigned int num,  long unsigned int startnum);

void main()
{

long unsigned int num;

printf("n Enter A Positive Number:");
scanf("%d", &num);
printf("n ");

/*Call To the primefind() function*/
primefind(num, 1);                                                  /*Find a prime less than the given number*/

if (num==0)
{
printf(" 0 n n");
}
else if (num==1)
{
printf(" 1 n n");
}
else
{
printf("b n n ");                                        /*Delete the extra 'x' left in the end*/
}

}

void primefind(long unsigned int num, long unsigned int startnum)
{

long unsigned int counter1, counter2, primecheck;
char primestat;

for(counter1 = startnum; counter1<=num; counter1++)
{

for (counter2 = 2; counter2< counter1; counter2++)
{

primecheck = counter1 % counter2;                    /* Check If The Chosen Number (counter1) is divisible
by numbers less than it excluding 1*/
if (primecheck != 0 )
primestat = 't';                                    /*The Number Is Prime*/
else                                                  /*(So Far)*/
{
primestat = 'f';                                   /*The Number Is Not Prime*/
break;
}

}

if (primestat =='t' || counter1 ==2)
{
factorcheck(counter1,num);                              /*This Calls The Function factorcheck()*/
break;                                                       /* which checks if the prime number */
}                                                                 /*generated is a factor of the given number*/

}

}

void factorcheck(long unsigned int prime, long unsigned int num)
{
long unsigned int remainder;

remainder = num%prime;

if (remainder == 0)                              /*since the remainder is 0, the prime number is a factor*/
{
printf("%dx", prime);
primefind((num/prime), 1);               /*Find Another prime number*/
return;
}
else
primefind(num, prime+1);               /*Since The Generated Prime is not a factor,*/
/*the function checks for the next prime number available*/
}

```

Is This A Good Question/Topic? 0

## Replies To: Prime Factors

### #2 benjaminjkaiser

Reputation: 0
• Posts: 0
• Joined: 14-November 10

## Re: Prime Factors

Posted 13 November 2010 - 11:54 PM

thank you very much for this... I'm writing an iPhone app and I needed the prime factors of a number and i wrote a function that used recursion but still wasn't working and I couldn't work out why... so yeah thanks very much for the code XD