|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r60943 - sandbox/xint/libs/xint/example
From: pbristow_at_[hidden]
Date: 2010-03-30 09:53:52
Author: pbristow
Date: 2010-03-30 09:53:51 EDT (Tue, 30 Mar 2010)
New Revision: 60943
URL: http://svn.boost.org/trac/boost/changeset/60943
Log:
Fibonacci sequence from original docs.
Added:
sandbox/xint/libs/xint/example/xint_fibonacci.cpp (contents, props changed)
Added: sandbox/xint/libs/xint/example/xint_fibonacci.cpp
==============================================================================
--- (empty file)
+++ sandbox/xint/libs/xint/example/xint_fibonacci.cpp 2010-03-30 09:53:51 EDT (Tue, 30 Mar 2010)
@@ -0,0 +1,60 @@
+// xint_fibonacci.cpp
+
+// Copyright Chad Nelson 2010
+//
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+/**
+\file
+\brief Simple demo of xint sending the fibonacci sequence to stream output.
+\details
+
+If you're not intimately familiar with the Fibonacci sequence,
+and how fast the numbers in it grow, the answer might shock you.
+
+As you can see, using the XInt library is just like using the native integer types
+-- except that an xint::integer won't overflow on you
+until you completely exhaust your system's memory. :-)
+
+**/
+#include <iostream>
+#include <limits>
+
+#include <boost/xint/xint.hpp>
+
+int main() {
+ using xint::integer;
+ using std::cout;
+ using std::endl;
+ using std::numeric_limits;
+
+//[xint_fibonacci_snippet_1
+
+// Select the limit, in this case the largest number
+// that will fit into an unsigned long:
+ integer limit=(std::numeric_limits<unsigned long>::max)();
+
+ // Set the first two numbers in the sequence:
+ integer n, n1=0, n2=1;
+//] [xint_fibonacci_snippet_1]
+
+ // Now count the items in the sequence as we iterate over them, until we
+ // come to the first one greater than the limit we've set.
+ size_t counter=0;
+ cout << counter++ << ": " << n1 << endl;
+ cout << counter++ << ": " << n2 << endl;
+
+ while (true) {
+ n = n1 + n2;
+ if (n > limit) break;
+ // That one didn't do it, go back for another try.
+ cout << counter++ << ": " << n << endl;
+
+ n1=n2;
+ n2=n;
+ }
+
+ cout << "The answer is " << counter << " (" << n << ")." << endl;
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk