-
powers without .math?
Hey guys, sorry if this has been asked before, but I couldn't find an answer anywhere.
How would one write 3^i without using .math? (i being numbers 0-9)
More specifically, this is what I have:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x, n, answer1, first;
x = 3;
n = 10;
first = 1;
float answer2;
int i;
answer2 = 1.0;
answer1 = 1;
answer1 = x;
for (i = 0; i < n; i++) {
printf("%5d %d %1.9f\n", answer1, i, answer2);
answer1 = x << i;
answer2 = 1.00/answer1;
}
return EXIT_SUCCESS;
}
And this is what I want it to produce:
1 0 1.0
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805
I believe I am only having troubles with the first column, any help would be appreciated :)
-
I would write a function. http://www.cprogramming.com/tutorial/c/lesson4.html
I think a for loop and a if statement would be needed in the function.
Tim S.
-
1 = 1
3 = 1 * 3
9 = 1 * 3 * 3
27 = 1 * 3 * 3 * 3
I decided maybe the math was your issue.
Tim S.
-
Bit shifting to the left (line 17) doubles a number. This has nothing to do with calculating 3i.
Plan on paper how you would calculate 3 raised to a power (hint: the math has already been provided for you in this thread) and try putting that in code.
-
Ignore what was previously here. Except for the thank yous :)
So I have now got my code to read:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x, n, answer1, first;
x = 3;
n = 10;
first = 1;
float answer2;
int i;
answer2 = 1.0;
answer1 = 1;
answer1 = x;
for (i = 0; i < n; i++) {
printf("%5d %d %1.9f\n", answer1, i, answer2);
answer1 = answer1*x;
answer2 = 1.00/answer1;
}
return EXIT_SUCCESS;
}
Which is basically what I want EXCEPT, I want the whole first column knocked down 1 (so it starts at 0 and ends at 19683) I am sure this is a REALLY simple fix, but I stuck myself...
-
101 decimal is 1100101 in binary, you can right shift it to get 50, 25, 12, 6, 3, 2, 1, 0. I don't think that's the answer though, I can think of any way to actually change the first number to 0 while maintaining everything else. The best I can think of is to add if/else logic into the for loop (you can either print a column of 0's before the 101, or print a column of 0's instead of the 101).
-
ummm... sorry but I'm really lost as to where binary came in here... :o
I feel confused haha
Basically I want these results:
Code:
1 0 1.0
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805
But I get this:
Code:
3 0 1.000000000
9 1 0.111111112
27 2 0.037037037
81 3 0.012345679
243 4 0.004115226
729 5 0.001371742
2187 6 0.000457247
6561 7 0.000152416
19683 8 0.000050805
59049 9 0.000016935
-
Code:
answer1 = 1;answer1 = x;
You are beginning this number at 3 instead of 1. Think that should be it.
Sorry about the shifting thing, I thought you were trying to do it through bit shifting for some reason.
-
here is one problem:
Code:
answer1 = 1;
answer1 = x;
(edited)
Alpo already had it. I type too slow.
-
-
Quote:
Originally Posted by
Alpo
Code:
answer1 = 1;answer1 = x;
You are beginning this number at 3 instead of 1. Think that should be it.
Sorry about the shifting thing, I thought you were trying to do it through bit shifting for some reason.
Wow thank you so much! That was a really simple fix.... I almost feel dumb xD
No worries! it was probably because my initial code that I posted had bit shifting in it for whatever reason :p
As for the i/answer1 thing, no I have that correct. I never really specified here what the other two columns were for as I had them figured out, but the three were 3^k, k, and 3^-k (or 1/3^k) so 1.00/answer1 was what I needed :)
-
Quote:
Originally Posted by
megafiddle
here is one problem:
Code:
answer1 = 1;
answer1 = x;
-
Ya I saw that, along with my useless int first :p Thanks though :)