bellyoshi

πの計算

• R/O
• SSH
• HTTPS

コミット

πの計算を行う。

コミットメタ情報

リビジョン 4 (tree) 2014-09-12 21:16:01 bellyoshi

差分

--- CALCPI/CalcPI.Test/LongNumTest.vb (revision 3)
+++ CALCPI/CalcPI.Test/LongNumTest.vb (revision 4)
 @@ -59,15 +59,13 @@ 59 59 ''' 60 60 _ 61 61 Public Sub ldivTest1() 62 - Dim target As LongNum = New LongNum() ' TODO: 適切な値に初期化してください 63 - Dim d() As Integer = {2, 0, 0, 0} 62 + 63 + Dim d As New LongNum(2) 64 64 Dim e As Integer = 2 65 - Dim f() As Integer = {0, 0, 0, 0} 66 - target.ldiv(d, e, f) 67 - Assert.AreEqual(1, f(0)) 68 - Assert.AreEqual(0, f(1)) 69 - Assert.AreEqual(0, f(2)) 70 - Assert.AreEqual(0, f(3)) 65 + Dim f As New LongNum(0) 66 + d.ldiv(e, f) 67 + Assert.AreEqual(1, f.GetValue(0)) 68 + Assert.AreEqual(0, f.GetValue(1)) 71 69 End Sub 72 70 73 71 '''
 @@ -74,15 +72,14 @@ 74 72 '''ldiv のテスト 75 73 ''' 76 74 _ 77 - Public Sub ldivTest2() 78 - Dim target As LongNum = New LongNum() ' TODO: 適切な値に初期化してください 79 - Dim d() As Integer = {4 * 239, 0, 0, 0} 75 + Public Sub ldivTest2() 76 + Dim d As New LongNum(4 * 239) 80 77 Dim e As Integer = 239 * 239 81 - Dim f() As Integer = {0, 0, 0, 0} 82 - target.ldiv(d, e, f) 83 - Assert.AreEqual(0, f(0)) 84 - Assert.AreEqual(1673640, f(1)) 85 - Assert.AreEqual(16736401, f(2)) 86 - Assert.AreEqual(67364016, f(3)) 78 + Dim f As New LongNum(0) 79 + d.ldiv(e, f) 80 + Assert.AreEqual(0, f.GetValue(0)) 81 + Assert.AreEqual(1673640, f.GetValue(1)) 82 + Assert.AreEqual(16736401, f.GetValue(2)) 83 + Assert.AreEqual(67364016, f.GetValue(3)) 87 84 End Sub 88 85 End Class
--- CALCPI/CALCPI/LongNum.vb (revision 3)
+++ CALCPI/CALCPI/LongNum.vb (revision 4)
 @@ -4,7 +4,7 @@ 4 4 ''' 算出桁数 5 5 ''' 6 6 ''' 7 - Public Const L As Integer = 10000 7 + Public Const L As Integer = 1000 8 8 9 9 ''' 10 10 ''' 配列サイズ
 @@ -13,12 +13,6 @@ 13 13 Private Const L1 As Integer = (L / 8) + 1 14 14 15 15 ''' 16 - ''' 配列サイズ + 1 17 - ''' 18 - ''' 19 - Public Const L2 As Integer = L1 + 1 20 - 21 - ''' 22 16 ''' 計算項数 23 17 ''' 24 18 '''
 @@ -25,22 +19,43 @@ 25 19 Public Const N As Integer = (L / 1.39794) + 1 26 20 27 21 22 + Private array(L) As Integer 28 23 29 - Public Function displayString(ByVal s() As Integer) As String 24 + Public Function GetValue(ByVal idx As Integer) As Integer 25 + Return array(idx) 26 + End Function 27 + 28 + 29 + Public Sub New(ByVal initialValue As Integer) 30 + Initialize(initialValue) 31 + End Sub 32 + 33 + Private Sub Initialize(ByVal value As Integer) 34 + array(0) = value 35 + For k As Integer = 1 To array.Length - 1 36 + array(k) = 0 37 + Next 38 + End Sub 39 + 40 + 41 + 42 + Public Function displayString() As String 30 43 Dim sb As New System.Text.StringBuilder 31 - sb.Append(s(0)) 44 + sb.Append(array(0)) 32 45 sb.Append(".") 33 - For i As Integer = 1 To s.Length - 1 34 - sb.Append(s(i)) 46 + For i As Integer = 1 To array.Length - 1 47 + sb.Append(array(i)) 35 48 sb.Append(" ") 36 49 Next 37 50 Return sb.ToString 38 51 End Function 39 52 40 - Public Sub ladd(ByVal a() As Integer, ByVal b() As Integer, ByVal c() As Integer) 41 - 53 + Public Sub ladd(opeLongNum As LongNum, outLongNum As LongNum) 54 + Dim b() As Integer = opeLongNum.array 55 + Dim c() As Integer = outLongNum.array 56 + Dim a() As Integer = array 42 57 Dim carry As Integer = 0 43 - For i As Integer = L2 To 0 Step -1 58 + For i As Integer = array.Length - 1 To 0 Step -1 44 59 c(i) = a(i) + b(i) + carry 45 60 If (c(i) < 100000000) Then 46 61 carry = 0
 @@ -54,9 +69,12 @@ 54 69 55 70 56 71 57 - Public Sub lsub(ByVal a() As Integer, ByVal b() As Integer, ByVal c() As Integer) 72 + Public Sub lsub(opeLongNum As LongNum, outLongNum As LongNum) 73 + Dim b() As Integer = opeLongNum.array 74 + Dim c() As Integer = outLongNum.array 75 + Dim a() As Integer = array 58 76 Dim borrow As Integer = 0 59 - For i As Integer = L2 To 0 Step -1 77 + For i As Integer = array.Length - 1 To 0 Step -1 60 78 c(i) = a(i) - b(i) - borrow 61 79 If (c(i) >= 0) Then 62 80 borrow = 0
 @@ -66,11 +84,14 @@ 66 84 End If 67 85 Next 68 86 69 - End Sub 87 + End Sub 70 88 71 - Public Sub ldiv(ByVal d() As Integer, ByVal e As Integer, ByVal f() As Integer) 89 + Public Sub ldiv(ByVal e As Integer, ByVal outLongNum As LongNum) 90 + 91 + Dim d() As Integer = array 92 + Dim f() As Integer = outLongNum.array 72 93 Dim r As Long = 0 73 - For i As Integer = 0 To d.Length - 1 94 + For i As Integer = 0 To array.Length - 1 74 95 Dim w As Long = d(i) 75 96 f(i) = (w + r) \ e 76 97 r = ((w + r) Mod e) * 100000000
--- CALCPI/CALCPI/Module1.vb (revision 3)
+++ CALCPI/CALCPI/Module1.vb (revision 4)
 @@ -1,16 +1,16 @@ 1 1 ﻿Module Module1 2 2 3 - Private a(LongNum.L2) As Integer 4 - Private b(LongNum.L2) As Integer 5 - Private s(LongNum.L2) As Integer 6 - Private q(LongNum.L2) As Integer 7 - Private lnum As New LongNum 3 + Private a As LongNum 4 + Private b As LongNum 5 + Private s As LongNum 6 + Private q As LongNum 7 + 8 8 Sub Main() 9 9 10 10 Try 11 11 12 12 Calc() 13 - Console.WriteLine(lnum.displayString(s)) 13 + Console.WriteLine(s.displayString) 14 14 Catch ex As Exception 15 15 Console.WriteLine("例外発生") 16 16 Console.Write(ex.ToString)
 @@ -19,25 +19,22 @@ 19 19 End Sub 20 20 21 21 Private Sub Calc() 22 - For k As Integer = 0 To LongNum.L2 23 - s(k) = 0 24 - a(k) = 0 25 - b(k) = 0 26 - q(k) = 0 27 - Next 28 22 29 - a(0) = 16 * 5 30 - b(0) = 4 * 239 31 23 24 + a = New LongNum(16 * 5) 25 + b = New LongNum(4 * 239) 26 + s = New LongNum(0) 27 + q = New LongNum(0) 28 + 32 29 For k As Integer = 1 To LongNum.N 33 - lnum.ldiv(a, 5 * 5, a) 34 - lnum.ldiv(b, 239 * 239, b) 35 - lnum.lsub(a, b, q) 36 - lnum.ldiv(q, 2 * k - 1, q) 30 + a.ldiv(5 * 5, a) 31 + b.ldiv(239 * 239, b) 32 + a.lsub(b, q) 33 + q.ldiv(2 * k - 1, q) 37 34 If k Mod 2 = 0 Then 38 - lnum.lsub(s, q, s) 35 + s.lsub(q, s) 39 36 Else 40 - lnum.ladd(s, q, s) 37 + s.ladd(q, s) 41 38 End If 42 39 Next 43 40