Finding the algorithm that's right for you.

This is a discussion on Finding the algorithm that's right for you. within the A Brief History of Cprogramming.com forums, part of the Community Boards category; 1 2 3 4 5 6 7 8 9 10 1 2 4 8 16 32 64 128 256 512 ...

  1. #1
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    Finding the algorithm that's right for you.

    1 2 3 4 5 6 7 8 9 10
    1 2 4 8 16 32 64 128 256 512

    I need an equation that takes a number from the top row as it's only variable, and produces the corresponding number on the bottom. You can also move them around, like have 0 above the 2 or something, but just tell me which direction you moved which bar how far. I just need a starting point. i tried finding the differences but it just turns into circular logic.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Well, if you had this

    0 1 2 3 4 5 6 7 8 9 10
    1 2 4 8 16 32 64 128 256 512

    Then it's just pow(2,n)

    Two, to the power of the number on the top row, gives you the number on the bottom row

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    A post in straight C from me... don't get these so often . It's a simple function you want.

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      for (int i = 1; i < 11; i++) {
        printf("Input is %i, output is is %i \n", i, 1 << (i - 1));
      }
      getchar();
      return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The annoying thing is, as soon as I logged off after posting this, I figured it out. Murphy's Law I guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on finding a better algorithm
    By Masterx in forum C Programming
    Replies: 15
    Last Post: 05-31-2009, 12:41 PM
  2. Need help finding a simple 'shortest path' algorithm
    By ashley in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:38 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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