0
\$\begingroup\$

I have written the following code to decompose a number into primes. The problem is that it takes so much time and I have no idea why.

#include <stdio.h>
#include <stdlib.h>
int prime (int n){int j=2; 
if (n==2) return 1;
while(j<(n/2)){ 
if(n%j==0) { return 0;}
j=j+1;
}return 1;}

int expotent (int n,int p){int j=0;
while((n%p)==0){
j=j+1;
p=p*p;
}
return j;
}

int main(int argc, char *argv[]) {int h=0,n=1,k=1,t=0 ;
scanf("%d",&n);
int j=n;
while(k!=n){
if(expotent(n,j)!=0 & prime(j)==1)
{
    if(t==0) {t=1; printf("%d^%d",j,expotent(n,j));
for( h<expotent(n,j); h++;){k=k*j;} 
    }
else {
printf("*%d^%d",j,expotent(n,j));
for( h<expotent(n,j); h++;){k=k*j;}}
}
j=j-1;
}
return 0;}
\$\endgroup\$
6
  • 2
    \$\begingroup\$ In your opinion, do you think your code is readable as formatted as it is ? \$\endgroup\$
    – Heslacher
    Commented Nov 10, 2014 at 17:10
  • \$\begingroup\$ Are you trying to write for C or C++? You have both tagged \$\endgroup\$
    – flakes
    Commented Nov 10, 2014 at 17:28
  • 1
    \$\begingroup\$ it is C. but well, you can run it as C++ as well I suppose. \$\endgroup\$
    – Iman
    Commented Nov 10, 2014 at 17:37
  • \$\begingroup\$ It could be a good idea to tell us : 1) how your program is supposed to be used 2) how your program is supposed to work : documentation of the different functions and of the whole algorithm. \$\endgroup\$
    – SylvainD
    Commented Nov 10, 2014 at 18:16
  • 1
    \$\begingroup\$ @Iman but you cannot run all C++ as C. So if we give you responses with C++ specific code, then it wont be useful to you as part of your C project :p \$\endgroup\$
    – flakes
    Commented Nov 10, 2014 at 18:54

1 Answer 1

3
\$\begingroup\$

Before you can hope to understand what the code does and why it performs badly, you first have to be able to read it easily. For your benefit and for the benefit of other reviewers, here is your code, with no changes other than whitespace:

#include <stdio.h>
#include <stdlib.h>

int prime(int n) {
    int j = 2;
    if (n == 2) return 1;
    while (j < (n/2)) { 
        if (n % j == 0) {
            return 0;
        }
        j = j + 1;
    }
    return 1;
}

int expotent(int n, int p){
    int j = 0;
    while ((n % p) == 0) {
        j = j + 1;
        p = p * p;
    }
    return j;
}

int main(int argc, char *argv[]) {
    int h = 0, n = 1, k = 1, t = 0;
    scanf("%d", &n);
    int j = n;
    while (k != n) {
        if (expotent(n, j) != 0 & prime(j) == 1) {
            if (t == 0) {
                t = 1;
                printf("%d^%d", j, expotent(n,j));
                for (h < expotent(n,j); h++; ) {
                    k = k * j;
                } 
            } else {
                printf("*%d^%d", j, expotent(n,j));
                for (h < expotent(n,j); h++; ) {
                    k = k * j;
                }
            }
        }
        j = j - 1;
    }
    return 0;
}

Now you can see what the branching and looping structure looks like!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ I was about to post the same comment ! Thanks for saving everyone some time. \$\endgroup\$
    – SylvainD
    Commented Nov 10, 2014 at 18:14
  • 1
    \$\begingroup\$ Well.. it is still not readable enough. \$\endgroup\$
    – Edenia
    Commented Nov 10, 2014 at 20:34
  • \$\begingroup\$ thanks a lot! im so sorry for it being unclear. let me explain; I defined two functions, prime, is used to determine if a number is prime or not. and expotent, which takes two numbers as argument, and returns the power of the second number in the prime decomposition of the first(like how many times can I divide the first by second?). the rest is just checking if a number j divides the number I want to decompose, if it does, AND is prime, I write it down. \$\endgroup\$
    – Iman
    Commented Nov 11, 2014 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.