Skip to main content
edited tags
Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308
deleted 221 characters in body
Source Link
user65308
user65308

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(long);
void main()
{
    printf("Enter a number: ");
    scanf("%ld",&n);
    pf(n);
}
void pf(long n)
{
    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;
    }
}

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;
    }
}

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>
void pf(long);
void main()
{
    printf("Enter a number: ");
    scanf("%ld",&n);
    pf(n);
}
void pf(long n)
{
    long i,x;
    while(n%2==0)
    {
        printf("2\n");
        n=n/2;
    }
    x=3;
    while(x<=n)
    {
        for(i=2;i<x;i++)
        {
            if(x%i==0)
            {
                goto abc;
            }
        }
        while(n%x==0)
        {
            printf("%ld\n",x);
            n=n/x;
        }
        abc:
            x=x+2;
    }
}
Rollback to Revision 2
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
  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;
        }
    }
    

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;
    }
}
  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;
        }
    }
    

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;
    }
}
deleted 118 characters in body; edited tags
Source Link
user65308
user65308
Loading
Rollback to Revision 1
Source Link
user65308
user65308
Loading
deleted 316 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Source Link
user65308
user65308
Loading