有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13, ...
(从第二项开始,后面的每一项的分子为前一项分子与分母的和,分母为前一项的分子)求前20项之和,用分数表示。
原题在C语言版:http://topic.csdn.net/u/20100322/21/a1dc6eba-f18e-4af3-a082-fd00636fb533.html
但解答都不是准确值,而是近似值。
(从第二项开始,后面的每一项的分子为前一项分子与分母的和,分母为前一项的分子)求前20项之和,用分数表示。
原题在C语言版:http://topic.csdn.net/u/20100322/21/a1dc6eba-f18e-4af3-a082-fd00636fb533.html
但解答都不是准确值,而是近似值。
{
double sum= 0;
for (int i = 0; i < 20; i++)
{
double a= Get(2 + i);
double b= Get(1 + i);
double c = a/ b;
sum +=c;
Console.WriteLine(c.ToString());
}
Console.WriteLine(sum.ToString());
}
static double Get(int n)
{
if (n == 0 || n == 1)
return 1;
return Get(n - 2) + Get(n - 1);
}
using Skyiv.Numeric;// S = 2/1 + 3/2 + 5/3 + 8/5 + 13/8 + 21/13 + ...
class Program
{
static void Main()
{
BigInteger p0 = 1;
BigInteger q0 = 2;
BigInteger pt = p0;
BigInteger qt = q0;
Console.WriteLine("S1 = 2/1");
for (int i = 2; i <= 20; i++)
{
BigInteger p1 = q0;
BigInteger q1 = p0 + q0;
BigInteger p2 = pt * p1;
BigInteger q2 = qt * p1 + q1 * pt;
BigInteger g = Gcd(p2, q2);
p2 /= g;
q2 /= g;
Console.WriteLine("S{0} = S{1} + {2}/{3} = {4}/{5}", i, i - 1, q1, p1, q2, p2);
p0 = p1;
q0 = q1;
pt = p2;
qt = q2;
}
}
// 求最大公约数
static BigInteger Gcd(BigInteger a, BigInteger b)
{
if (b == 0) return a;
for (BigInteger c = a % b; c > 0 ; c = a % b)
{
a = b;
b = c;
}
return b;
}
}/* 程序输出:
S1 = 2/1
S2 = S1 + 3/2 = 7/2
S3 = S2 + 5/3 = 31/6
S4 = S3 + 8/5 = 203/30
S5 = S4 + 13/8 = 1007/120
S6 = S5 + 21/13 = 15611/1560
S7 = S6 + 34/21 = 42319/3640
S8 = S7 + 55/34 = 819523/61880
S9 = S8 + 89/55 = 10116217/680680
S10 = S9 + 144/89 = 998361233/60580520
S11 = S10 + 233/144 = 19734909839/1090449360
S12 = S11 + 377/233 = 5009333401207/254074700880
S13 = S12 + 610/377 = 157192635368603/7368166325520
S14 = S13 + 987/610 = 10315988773813607/449458145856720
S15 = S14 + 1597/987 = 519031694223200569/21124532855265840
S16 = S15 + 2584/1597 = 883479408572458239253/33735878969859546480
S17 = S16 + 4181/2584 = 17823238983266285691637/640981700427331383120
S18 = S17 + 6765/4181 = 78855203392427237283541097/2679944489486672512824720
S19 = S18 + 10946/6765 = 682169846462656215210345341/21975544813790714605162704
S20 = S19 + 17711/10946 = 302163077445280087617864490505/9251704366605890848773498384
*/
{
using System;
using System.Text;
using System.Collections.Generic; sealed class BigInteger : IEquatable<BigInteger>, IComparable<BigInteger>
{
static readonly int Len = 9; // int.MaxValue = 2,147,483,647
static readonly int Base = (int)Math.Pow(10, Len); int sign;
List<int> data; BigInteger(long x)
{
sign = (x == 0) ? 0 : ((x > 0) ? 1 : -1);
data = new List<int>();
for (ulong z = (x < 0) ? (ulong)-x : (ulong)x; z != 0; z /= (ulong)Base) data.Add((int)(z % (ulong)Base));
} BigInteger(BigInteger x)
{
sign = x.sign; // x != null
data = new List<int>(x.data);
} public static implicit operator BigInteger(long x)
{
return new BigInteger(x);
} public static BigInteger Parse(string s)
{
if (s == null) return null;
s = s.Trim().Replace(",", null);
if (s.Length == 0) return 0;
BigInteger z = 0;
z.sign = (s[0] == '-') ? -1 : 1;
if (s[0] == '-' || s[0] == '+') s = s.Substring(1);
int r = s.Length % Len;
z.data = new List<int>(new int[s.Length / Len + ((r != 0) ? 1 : 0)]);
int i = z.data.Count - 1;
if (r != 0) z.data[i--] = int.Parse(s.Substring(0, r));
for (; i >= 0; i--, r += Len) z.data[i] = int.Parse(s.Substring(r, Len));
z.Shrink();
return z;
} public static BigInteger Abs(BigInteger x)
{
if (x == null) return null;
BigInteger z = new BigInteger(x);
z.sign = Math.Abs(x.sign);
return z;
} public static BigInteger Pow(BigInteger x, long y)
{
if (x == null) return null;
BigInteger z = 1, n = x;
for (; y > 0; y >>= 1, n *= n) if ((y & 1) != 0) z *= n;
return z;
} public static BigInteger operator +(BigInteger x)
{
if (x == null) return null;
return new BigInteger(x);
} public static BigInteger operator -(BigInteger x)
{
if (x == null) return null;
BigInteger z = new BigInteger(x);
z.sign = -x.sign;
return z;
} public static BigInteger operator ++(BigInteger x)
{
return x + 1;
} public static BigInteger operator --(BigInteger x)
{
return x - 1;
} public static BigInteger operator +(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
if (x.AbsCompareTo(y) < 0) Swap(ref x, ref y);
BigInteger z = new BigInteger(x);
if (x.sign * y.sign == -1) z.AbsSubtract(y);
else z.AbsAdd(y);
return z;
} public static BigInteger operator -(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
return x + (-y);
} public static BigInteger operator *(BigInteger x, BigInteger y)
{
if (x == null || y == null) return null;
BigInteger z = 0;
z.sign = x.sign * y.sign;
z.data = new List<int>(new int[x.data.Count + y.data.Count]);
for (int i = x.data.Count - 1; i >= 0; i--)
for (int j = y.data.Count - 1; j >= 0; j--)
{
long n = Math.BigMul(x.data[i], y.data[j]);
z.data[i + j] += (int)(n % Base);
z.CarryUp(i + j);
z.data[i + j + 1] += (int)(n / Base);
z.CarryUp(i + j + 1);
}
z.Shrink();
return z;
} public static BigInteger operator /(BigInteger dividend, BigInteger divisor)
{
BigInteger remainder;
return DivRem(dividend, divisor, out remainder);
} public static BigInteger operator %(BigInteger dividend, BigInteger divisor)
{
BigInteger remainder;
DivRem(dividend, divisor, out remainder);
return remainder;
} public static BigInteger DivRem(BigInteger dividend, BigInteger divisor, out BigInteger remainder)
{
remainder = null;
if (dividend == null || divisor == null) return null;
if (divisor.sign == 0) throw new DivideByZeroException();
if (dividend.AbsCompareTo(divisor) < 0)
{
remainder = new BigInteger(dividend);
return 0;
}
remainder = 0;
BigInteger quotient = 0;
quotient.sign = dividend.sign * divisor.sign;
quotient.data = new List<int>(new int[dividend.data.Count]);
divisor = Abs(divisor); // NOT: divisor.sign = Math.Abs(divisor.sign);
for (int i = dividend.data.Count - 1; i >= 0; i--)
{
remainder = remainder * Base + dividend.data[i];
int iQuotient = remainder.BinarySearch(divisor, -1);
quotient.data[i] = iQuotient;
remainder -= divisor * iQuotient;
}
quotient.Shrink();
if (remainder.sign != 0) remainder.sign = dividend.sign;
return quotient;
} public static BigInteger Sqrt(BigInteger x)
{
if (x == null || x.sign < 0) return null;
BigInteger root = 0;
root.sign = 1;
root.data = new List<int>(new int[x.data.Count / 2 + 1]);
for (int i = root.data.Count - 1; i >= 0; i--) root.data[i] = x.BinarySearch(root, i);
root.Shrink();
return root;
} int BinarySearch(BigInteger divisor, int i)
{
int low = 0, high = Base - 1, mid = 0, cmp = 0;
while (low <= high)
{
mid = (low + high) / 2;
cmp = CompareTo(divisor, mid, i);
if (cmp > 0) low = mid + 1;
else if (cmp < 0) high = mid - 1;
else return mid;
}
return (cmp < 0) ? (mid - 1) : mid;
} int CompareTo(BigInteger divisor, int mid, int i)
{
if (i >= 0) divisor.data[i] = mid;
return AbsCompareTo(divisor * ((i >= 0) ? divisor : mid));
} void AbsAdd(BigInteger x)
{
for (int i = 0; i < x.data.Count; i++)
{
data[i] += x.data[i];
CarryUp(i);
}
} void AbsSubtract(BigInteger x)
{
for (int i = 0; i < x.data.Count; i++)
{
data[i] -= x.data[i];
CarryDown(i);
}
Shrink();
} void CarryUp(int n)
{
for (; data[n] >= Base; n++)
{
if (n == data.Count - 1) data.Add(data[n] / Base);
else data[n + 1] += data[n] / Base;
data[n] %= Base;
}
} void CarryDown(int n)
{
for (; data[n] < 0; n++)
{
data[n + 1]--;
data[n] += Base;
}
Shrink();
} void Shrink()
{
for (int i = data.Count - 1; i >= 0 && data[i] == 0; i--) data.RemoveAt(i);
if (data.Count == 0) sign = 0;
} public static bool operator ==(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return object.ReferenceEquals(y, null);
return x.Equals(y);
} public static bool operator !=(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return !object.ReferenceEquals(y, null);
return !x.Equals(y);
} public static bool operator <(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return !object.ReferenceEquals(y, null);
return x.CompareTo(y) < 0;
} public static bool operator >(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return false;
return x.CompareTo(y) > 0;
} public static bool operator <=(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return true;
return x.CompareTo(y) <= 0;
} public static bool operator >=(BigInteger x, BigInteger y)
{
if (object.ReferenceEquals(x, null)) return object.ReferenceEquals(y, null);
return x.CompareTo(y) >= 0;
} public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (sign < 0) sb.Append('-');
sb.Append((data.Count == 0) ? 0 : data[data.Count - 1]);
for (int i = data.Count - 2; i >= 0; i--) sb.Append(data[i].ToString("D" + Len));
return sb.ToString();
} public override int GetHashCode()
{
int hash = sign;
foreach (int n in data) hash ^= n;
return hash;
} public override bool Equals(object other)
{
if (other == null || GetType() != other.GetType()) return false;
return Equals(other as BigInteger);
} public bool Equals(BigInteger other)
{
return CompareTo(other) == 0;
} public int CompareTo(BigInteger other)
{
if (object.ReferenceEquals(other, null)) return 1;
if (sign < other.sign) return -1;
if (sign > other.sign) return 1;
if (sign == 0) return 0;
return sign * AbsCompareTo(other);
} int AbsCompareTo(BigInteger other)
{
if (data.Count < other.data.Count) return -1;
if (data.Count > other.data.Count) return 1;
for (int i = data.Count - 1; i >= 0; i--)
if (data[i] != other.data[i])
return (data[i] < other.data[i]) ? -1 : 1;
return 0;
} public static void Swap<T>(ref T x, ref T y)
{
T z = x;
x = y;
y = z;
}
}
}
原贴的楼主改了个,你试试,在C#中基本不用改
now 1/2+2/1 this is 1result
2/1:
now 2/1+3/2 this is 2result
7/2:
now 7/2+5/3 this is 3result
31/6:
now 31/6+8/5 this is 4result
203/30:
now 203/30+13/8 this is 5result
2014/240:
now 2014/240+21/13 this is 6result
31222/3120:
now 31222/3120+34/21 this is 7result
761742/65520:
now 761742/65520+55/34 this is 8result
29502828/2227680:
now 29502828/2227680+89/55 this is 9result
1820919060/122522400:
now 1820919060/122522400+144/89 this is 10result
179705021940/10904493600:
now 179705021940/10904493600+233/144 this is 11result
28418270168160/1570247078400:
now 28418270168160/1570247078400+377/233 this is 12result
7213440097738080/365867569267200:
now 7213440097738080/365867569267200+610/377 this is 13result
2942646134100248160/137932073613734400:
now 2942646134100248160/137932073613734400+987/610 this is 14result
12691714792113862336/10351588609539777536:
now 12691714792113862336/10351588609539777536+1597/987 this is 15result
4587593158863055424/15968484854378384384:
now 4587593158863055424/15968484854378384384+2584/1597 this is 16result
227248267085803840/8270002575679527936:
now 227248267085803840/8270002575679527936+4181/2584 this is 17result
4596086575418042880/8357018200239415296:
now 4596086575418042880/8357018200239415296+6765/4181 this is 18result
9134929791062823424/2559819595104591872:
now 9134929791062823424/2559819595104591872+10946/6765 this is 19result
388429663056275968/14133619743004598272:
now 388429663056275968/14133619743004598272+17711/10946 this is 20result
7222142976624440320/12205904800032833536:
static ulong x = 1;
static ulong y = 2;
static ulong resultx = 0;
static ulong resulty = 0;
static void Main(string[] args)
{
string sum="";
for (int n = 1; n < 21; n++)
{
sum = Get();
Console.Write("this is {1}result\n{0}:\n", sum, n);
}
Console.ReadKey();
}
static string Get()
{
string result = resulty + "/" + resultx;
ulong tempx = y + x;
ulong tempy = y;
if (x == 1 && y == 2)
{
resultx = 1;
resulty = 2;
result=x+"/"+y;
Console.Write("now {0}+{1}/{2}\t", result, y, x);
}
else
{
try
{
resulty = resulty * x + resultx * y;
resultx = resultx * x;
Console.Write("now {0}+{1}/{2}\t", result,y,x);
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadKey();
}
}
x = tempy;
y = tempx;
return result = resulty + "/" + resultx;
}学习下高手的方法 顺便贴下自己简单的代码献丑了哈~
{
static double fa = 1, fb = 2;
static void Main(string[] args)
{
for (int i = 0; i < 18; i++)
{
f(i);
}
Console.WriteLine("fa is {0},fb is{1}", fa, fb);
} public static void f(int n)
{
System.Console.WriteLine("g(n) is {0},fa is {1},fb is {2},g(n+1) is {3}", g(n), fa, fb, g(n + 1));
fa = fa * g(n);
fb = fb * g(n) + fa * g(n + 1);
} public static int g(int a)
{
if (a == 0)
return 2;
if (a == 1)
return 3;
return g(a - 1) + g(a - 2);
}
}
thanks