Power

This is a discussion on Power within the C++ Programming forums, part of the General Programming Boards category; I would like not to use the function pow() but instead write a very simple one Code: long power2 (long ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Power

    I would like not to use the function pow()
    but instead write a very simple one

    Code:
    long power2 (long n);
    {
    // Returns 2 raised to the power of n.
    }
    is there a simple formula i can use?

    thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Raising a number to a power is just repeated multiplication.

    (If you want 2^n, you can do even better because of the internal binary representation of the number -- multiplying by 2 in binary is just like multiplying by 10 in decimal: you just move things over.)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    If your comment is accurate then in this case it really is very simple:
    Code:
    // Returns 2 raised to the power of n.
    long power2(long n)
    {
        return 1L << n;
    }
    However, if what you want is to implement your own version of pow(), then recall that pow() takes two arguments, not just one, and for good reason.

    EDIT:
    Then again, since your power2() function deals with long rather than unsigned long, it might make sense to define power2() for negative valued arguments as returning 0. A simple if statement would suffice.
    Last edited by laserlight; 01-23-2009 at 02:13 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    what i want to do is send in a prime number
    and make it a mersenne prime.
    using 2^n -1

    not sure how i can do that without using pow()

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Ah, then a left shift as tabstop hinted and I demonstrated would indeed be correct. Since n would be positive, you would not have to account for negative numbers either.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Quote Originally Posted by laserlight View Post
    Ah, then a left shift as tabstop hinted and I demonstrated would indeed be correct. Since n would be positive, you would not have to account for negative numbers either.
    i'm not sure how to do that

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by mrsirpoopsalot
    i'm not sure how to do that
    Read the tutorial on bitwise operations. The expression that you are looking for is (1L << n) - 1. Note that you have a pretty limited set of prime number inputs since this would quickly overflow even an unsigned long long.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I was going to test from 2 to a million numbers and if a prime number then send that prime number to the power2 function. I will work on it this weekend!
    ok, i will read the tutorial thank you and i will use this expression
    (1L << n) - 1

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by mrsirpoopsalot View Post
    I was going to test from 2 to a million numbers and if a prime number then send that prime number to the power2 function. I will work on it this weekend!
    ok, i will read the tutorial thank you and i will use this expression
    (1L << n) - 1
    You wont get anywhere near 1 million unless you use a bignum library of some kind, e.g. GMP. Otherwise you'll be limited to about 2 to 31.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. No Power, but power being supplied.
    By jrahhali in forum Tech Board
    Replies: 6
    Last Post: 08-11-2005, 02:50 AM
  3. The destructive power of a nuclear bomb
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 03-24-2005, 01:46 AM
  4. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 10:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21