Skip to main content
2 of 7
deleted 316 characters in body; edited tags
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Prime factors of a number in C

The following is code on "prime factors of a number". Any suggestions to optimize the code? It runs for a pretty long time for inputs like 35068499. I also want the code to take 10 digit numbers as input.

#include<stdio.h>
long n;
void pf();
void main()
{
    printf("Enter a number: ");
    scanf("%ld",&n);
    pf(n);
}
void pf()
{
    long i,x;
    while(1)
    {
        if(n%2==0)
        {
            printf("2\n");
            n=n/2;
        }
        else
            break;
    }
    x=3;
    while(x<=n)
    {
        for(i=2;i<x;i++)
        {
            if(x%i==0)
            {
                goto abc;
            }
        }
        while(1)
        {
            if(n%x==0)
            {
                printf("%ld\n",x);
                n=n/x;
            }
            else
                break;
        }
        abc:
            x=x+2;
    }
}
user65308