Skip to main content
3 of 7
Rollback to Revision 1
user avatar
user avatar

Prime factors of a number in C

  1. 1.The following is a c code on "prime factors of a number". Any suggestions to optimize the code? It runs for a pretty long time for inputs like 35068499. And 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;
        }
    }
    

It runs for a pretty long time for inputs like 35068499. And i also want the code to take 10 digit numbers as input.

user65308