C++ Algorithm

This is a discussion on C++ Algorithm within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to write a halving algorithm for the root function. Here's what's given: Code: global_procedure Root ( alters ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    C++ Algorithm

    Hi,

    I need to write a halving algorithm for the root function. Here's what's given:

    Code:
    global_procedure Root (
    alters Natural_Power_2& n,
    preserves Integer r
    );
    /*!
    requires
    r > 0
    ensures
    n^(r) <= #n < (n+1)^(r)
    !*/
    
    //------------------------------------...
    
    procedure_body Root (
    alters Natural_Power_2& n,
    preserves Integer r
    )
    {
    
    //Fill in here
    
    }


    I was thinking of doing something like:

    Code:
    object Natural_Power_2 old_self, temp;
    
    if (r mod 2 == 1)
    {
    self.Copy_To (old_self);
    }
    self.Power (r/2);
    self.Copy_To (temp);
    self.Divide (temp);
    if (r mod 2 == 1)
    {
    self.Divide (old_self);
    }
    }
    Would that work?

    Thanks!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    I have no idea what that language is, but it isn't C++.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Could be D, perhaps?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by CornedBee View Post
    Could be D, perhaps?
    Not to sound stupid (although this question will), but is 'D' seriously a language?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    a visit to wikipedia tells me it is

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe a better question is: does anyone actually use D?

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by cpjust View Post
    Maybe a better question is: does anyone actually use D?
    I have the D compiler on my hard drive and I've toyed around with a few basic programs.
    It's an extremely promising language. (Watch this space)
    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. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 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