Thread: powers without .math?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    12

    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

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    Last edited by stahta01; 09-22-2014 at 04:19 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    1 = 1
    3 = 1 * 3
    9 = 1 * 3 * 3
    27 = 1 * 3 * 3 * 3

    I decided maybe the math was your issue.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    12
    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...
    Last edited by DrPenguin; 09-22-2014 at 08:36 PM.

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    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).
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    12
    ummm... sorry but I'm really lost as to where binary came in here...

    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
    Last edited by DrPenguin; 09-22-2014 at 09:25 PM.

  8. #8
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    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.
    Last edited by Alpo; 09-22-2014 at 09:46 PM.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    here is one problem:

    Code:
    		answer1 = 1;
    		answer1 = x;
    (edited)
    Alpo already had it. I type too slow.

    -
    Last edited by megafiddle; 09-22-2014 at 10:00 PM.

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    12
    Quote Originally Posted by Alpo View Post
    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

    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
    Last edited by DrPenguin; 09-22-2014 at 10:01 PM.

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    12
    Quote Originally Posted by megafiddle View Post
    here is one problem:

    Code:
            answer1 = 1;
            answer1 = x;
    -
    Ya I saw that, along with my useless int first Thanks though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. powers
    By domacadin in forum C++ Programming
    Replies: 3
    Last Post: 12-24-2010, 01:51 PM
  2. Powers
    By Gordon in forum Windows Programming
    Replies: 8
    Last Post: 06-18-2008, 05:23 PM
  3. The powers (that be)
    By mlmoran in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 12:49 PM
  4. Help with powers
    By Tim in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 02:52 AM

Tags for this Thread