Fibonacci sequence: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added whitespace.) |
m (→Arbitrary Precision: noticed that the first digit was missing of the "partial" string. spruced it up a bit, removed string conversion) |
||
Line 2,215: | Line 2,215: | ||
<lang csharp>using System; |
<lang csharp>using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Numerics; |
using BI = System.Numerics.BigInteger; |
||
class Program |
|||
{ |
{ |
||
// A sparse array of values calculated along the way |
// A sparse array of values calculated along the way |
||
static SortedList<int, BI> sl = new SortedList<int, BI>(); |
|||
⚫ | |||
public static BigInteger sqr(BigInteger n) |
|||
{ |
|||
⚫ | |||
} |
|||
⚫ | |||
public static void IfNec(int n) |
|||
{ |
|||
⚫ | |||
} |
|||
// This routine is semi-recursive, but doesn't need to evaluate every number up to n. |
// This routine is semi-recursive, but doesn't need to evaluate every number up to n. |
||
// Algorithm from here: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html#section3 |
// Algorithm from here: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html#section3 |
||
static BI Fsl(int n) |
|||
{ |
{ |
||
if (n < 2) return n; |
if (n < 2) return n; |
||
int n2 = n >> 1, pm = n2 + ((n & 1) << 1) - 1; IfNec(n2); IfNec(pm); |
int n2 = n >> 1, pm = n2 + ((n & 1) << 1) - 1; IfNec(n2); IfNec(pm); |
||
return n2 > pm ? (2 * sl[pm] + sl[n2]) * sl[n2] : sqr(sl[n2]) + sqr(sl[pm]); |
return n2 > pm ? (2 * sl[pm] + sl[n2]) * sl[n2] : sqr(sl[n2]) + sqr(sl[pm]); |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
// Conventional iteration method (not used here) |
// Conventional iteration method (not used here) |
||
public static |
public static BI Fm(BI n) |
||
{ |
{ |
||
if (n < 2) return n; |
if (n < 2) return n; BI cur = 0, pre = 1; |
||
for (int i = 0; i <= n - 1; i++) { |
for (int i = 0; i <= n - 1; i++) { BI sum = cur + pre; pre = cur; cur = sum; } |
||
return cur; |
return cur; |
||
} |
} |
||
public static void Main() |
public static void Main() |
||
{ |
{ |
||
int num = 2_000_000; |
int num = 2_000_000, digs = 35, vlen; |
||
var sw = System.Diagnostics.Stopwatch.StartNew(); var v = Fsl(num); sw.Stop(); |
|||
DateTime st = DateTime.Now; |
|||
⚫ | |||
BigInteger v = Fsl(num); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if (vlen < 10000) { |
|||
sw.Restart(); Console.WriteLine(v); sw.Stop(); |
|||
Console.WriteLine("{0:n3} |
Console.WriteLine("{0:n3} ms to write it to the console.", sw.Elapsed.TotalMilliseconds); |
||
⚫ | |||
⚫ | |||
⚫ | |||
if (vs.Length < 10000) |
|||
{ |
|||
st = DateTime.Now; |
|||
Console.WriteLine(vs); |
|||
Console.WriteLine("{0:n3} ms to write it to the console.", (DateTime.Now - st).TotalMilliseconds); |
|||
} |
|||
⚫ | |||
⚫ | |||
} |
} |
||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre>137.209 ms to calculate the 2,000,000th Fibonacci number, number of digits is 417975 |
||
⚫ | |||
4.728 seconds to convert to a string. |
|||
number of digits is 417975 |
|||
⚫ | |||
</pre> |
</pre> |
||