## Help with my scripts

For scripts to aid with computation or simulation in cellular automata.
calcyman
Posts: 2198
Joined: June 1st, 2009, 4:32 pm

### Re: Help with my scripts

You seem to be resetting the variable bB within the loop in int power().
What do you do with ill crystallographers? Take them to the mono-clinic!

wildmyron
Posts: 1398
Joined: August 9th, 2013, 12:45 am

### Re: Help with my scripts

Rhombic wrote:A bit of a change of scene now that I feel very comfortable with Python...now my issues involve C.

Code: Select all

``````#include <stdio.h>

int factorial(int n){
int nN; int i;
for(i=0;i<n;i++){
nN=1;
nN*=(n-i+1);
}
return nN;
}
int power(int b, int exp){
int bB; int i;
for(i=0;i<exp;i++){
bB=1;
bB*=b;
}
return bB;
}
int main(){
printf("Showing the minimum n for which A^n > A! \n A \t n");
int i;
for(i=1;factorial(i)<1000000000;i++){
int c; c=1;
while (c>0){
if (power(i,c)>factorial(i)){
printf("%d \t %d",i,c);
break;
}
c++;
}
}
return 0;
}
``````
The script is fairly simple. For integer A, it gives the smallest integer n so that A^n > A!. It doesn't work and, unlike Python, C doesn't really help you much with the debugging. How can I find the issue?
In this case the problem is not so much with C syntax but a logic error - a Python version of this program would have the same behaviour. The main issue is that in both functions you use an intermediate value to help calculate the result (nN and bB) but inside the loop you set it to 1 on every iteration. You need to move the initialisation of these variables outside of the loop.
A minor point: for each value i of A, you are calculating factorial(i) as many times as you call power(), plus one extra in the main loop test. This is very inefficient so you'll want to store that result until the next i.
I don't have a compiler handy, so I'm assuming that the code compiles. If not then you really should state what the error messages are when asking for help. And in any case it's better to be descriptive rather than vaguely say "it doesn't work".
The latest version of the 5S Project contains over 226,000 spaceships. There is also a GitHub mirror of the collection. Tabulated pages up to period 160 (out of date) are available on the LifeWiki.

Rhombic
Posts: 1064
Joined: June 1st, 2013, 5:41 pm

### Re: Help with my scripts

I can't believe I missed that! It compiles fine, but it still does not print any numbers (so it's either stuck in a loop or "not finding anything"; the latter should actually be impossible).
The variables bB and nN are now set before the for loop, so even though that clearly was a problem there has to be something else. The program runs fine, just does not print anything.
Here it is.

Code: Select all

``````#include <stdio.h>

int factorial(int n){
int nN; int i; nN=1;
for(i=0;i<n;i++){
nN*=(n-i+1);
}
return nN;
}
int power(int b, int exp){
int bB; int i; bB=1;
for(i=0;i<exp;i++){
bB*=b;
}
return bB;
}
int main(){
printf("Showing the minimum n for which A^n > A! \n A \t n");
int i; int F;
for(i=1;F=factorial(i)<1000000000;i++){
int c; c=1;
while (c>0){
if (power(i,c)>F){
printf("%d \t %d",i,c);
break;
}
c++;
}
}
return 0;
}
``````
power(i,c) does return i^c, right? and factorial(i) is hopefully working OK too?
SoL : FreeElectronics : DeadlyEnemies : 6a-ite : Rule X3VI
what is “sesame oil”?

wildmyron
Posts: 1398
Joined: August 9th, 2013, 12:45 am

### Re: Help with my scripts

I suggest you try "running" the code with pencil and paper, just to help you identify what it's actually doing. Start with i=1 in the main loop and step through line by line, keeping track of all the variables. You will find problems with the factorial function and the while loop (hint: for what value of C will the loop stop for i=1?).

Edit: alternatively you can do the same thing with a debugger, but if you're using gcc in mingw then gdb is the only option I'm aware of and it's not very learner friendly.
The latest version of the 5S Project contains over 226,000 spaceships. There is also a GitHub mirror of the collection. Tabulated pages up to period 160 (out of date) are available on the LifeWiki.

Rhombic
Posts: 1064
Joined: June 1st, 2013, 5:41 pm

### Re: Help with my scripts

Thank you, found the problem (starting at 1...).
SoL : FreeElectronics : DeadlyEnemies : 6a-ite : Rule X3VI
what is “sesame oil”?