fork(1) download
  1. # 高校までの数学の計算問題
  2. #(1) 多倍長整数と分数計算
  3. p "(1)",x=Rational(12345678901234567890 ,(1 - 12345678901234567891i)) -
  4. Rational(12345678901234567891,(1 + 12345678901234567890i))
  5.  
  6. #(2) 多倍長整数と複素数の分数計算
  7.  
  8. p "(2-1)",x=Rational(12345678901234567890 ,(1 - 12345678901234567891i)) -
  9. Rational(12345678901234567891,(1 + 12345678901234567890i))
  10. p "(2-2)",x+0.0
  11.  
  12. #(3) 数桁の数字を虚数係数とする分数を要素とする行列の四則演算
  13. require 'matrix'
  14.  
  15. a = Matrix[[Rational(1, 2 + 3i), Rational(2, 3 + 4i)], [Rational(3, 4 + 5i), Rational(4, 5 + 6i)]]
  16. b = Matrix[[Rational(5, 6 + 7i), Rational(6, 7 + 8i)], [Rational(7, 8 + 9i), Rational(8, 9 + 10i)]]
  17.  
  18. # 行列の加算
  19. c = a + b
  20. puts "行列の加算: #{c}"
  21.  
  22. # 行列の減算
  23. d = a - b
  24. puts "行列の減算: #{d}"
  25.  
  26. # 行列の乗算
  27. e = a * b
  28. puts "行列の乗算: #{e}"
  29.  
  30. #(4) 整数係数の連立方程式の解
  31. require 'matrix'
  32. m = Matrix[ [1234567890123456789890, 1234567890123456789890*2],
  33. [1234567890123456789890, 1234567890123456789890*3] ]
  34. b = Vector[1234567890123456789890,0]
  35. p "(4-1)",m.lup.solve(b)
  36. p "(4-2)", Matrix[[1234567890123456789890,0]] / m
  37.  
  38. #(5) 多倍長整数と複素数の分数を係数とする連立方程式の解
  39.  
  40. m = Matrix[ [1234567890123456789890, 1234567890123456789890*(1+1i)],
  41. [1234567890123456789890, Rational(1234567890123456789890*(2 - 3i),10 + 10i)] ]
  42. b = Vector[1234567890123456789890 * Rational(1 + 1i, 2),0]
  43. p "(5)",m.lup.solve(b)
  44.  
  45. #(6) フィボナッチ数列の一般項
  46. require 'matrix'
  47. p "(6) フィボナッチ数列ランダムアクセス"
  48. [2,4,5,6,7,8,9,16,17,32,33,64,65,255,256,257].each {|n|
  49. f=(Matrix[[0,1],[1,1]] ** (n))[0,0]
  50. p [n,f.to_s().length, f]
  51. }
  52.  
  53. p "#(7) 一次方程式の解の法則"
  54. def SolvingLinearEquations(y,a,b)
  55. x= (y -b) / a
  56. end
  57.  
  58.  
  59. p "(7-1) 実数解",SolvingLinearEquations(1.0,5, 0.5)
  60. p "(7-2) 分数解",SolvingLinearEquations(Rational(1,1), Rational(5,1), Rational(1,2))
  61. p "(7-3) 虚数解",SolvingLinearEquations(1+1i, 5, 1.0 /(2+ 2i))
  62. p "(7-4) 虚数&分数解",SolvingLinearEquations(Rational(1+1i, 1), Rational(5,1), Rational(1,2+ 2i))
  63. p "(7-5) 多倍長整数の行列解",SolvingLinearEquations(Matrix[[Rational(1234567890123456789890,1),Rational(0,1)]],
  64. Matrix[ [Rational(1234567890123456789890,1), Rational(1234567890123456789890*2,1)],
  65. [Rational(1234567890123456789890,1), Rational(1234567890123456789890*3,1)] ],
  66. Matrix[[Rational(1234567890,1),Rational(123456789,1)]] )
  67. p "(7-7) 多倍長整数 複素数分数の行列解",SolvingLinearEquations(Matrix[[Rational(1234567890123456789890,1i),Rational(0,1)]],
  68. Matrix[ [Rational(1234567890123456789890,1), Rational(1234567890123456789890*2,1i)],
  69. [Rational(1234567890123456789890,1), Rational(1234567890123456789890*3,1i)] ],
  70. Matrix[[1234567890, 0+1i]] )
  71.  
  72.  
Success #stdin #stdout 0.03s 7232KB
stdin
Standard input is empty
stdout
"(1)"
((-228623681298582551271376318164380429986/11615286144559076666048468336368822614758804202921592629334943372565917420041)+(23230572289118153332096936672737645229441400512076991074912761305743708030085/11615286144559076666048468336368822614758804202921592629334943372565917420041)*i)
"(2-1)"
((-228623681298582551271376318164380429986/11615286144559076666048468336368822614758804202921592629334943372565917420041)+(23230572289118153332096936672737645229441400512076991074912761305743708030085/11615286144559076666048468336368822614758804202921592629334943372565917420041)*i)
"(2-2)"
(-1.9683000354294004e-38+(23230572289118153332096936672737645229441400512076991074912761305743708030085/11615286144559076666048468336368822614758804202921592629334943372565917420041)*i)
行列の加算: Matrix[[112/221-142/221i, 1728/2825-2104/2825i], [4036/5945-4758/5945i, 8012/11041-9224/11041i]]
行列の減算: Matrix[[-44/221+40/221i, -372/2825+296/2825i], [-556/5945+408/5945i, -772/11041+536/11041i]]
行列の乗算: Matrix[[-69753/801125-298546/801125i, -577052/6647225-2555814/6647225i], [-565109/6164965-3354018/6164965i, -4608696/51152953-28734070/51152953i]]
"(4-1)"
Vector[(3/1), (-1/1)]
"(4-2)"
Matrix[[(3/1), (-2/1)]]
"(5)"
Vector[((33/1066)+(113/1066)*i), ((230/533)-(20/533)*i)]
"(6) フィボナッチ数列ランダムアクセス"
[2, 1, 1]
[4, 1, 2]
[5, 1, 3]
[6, 1, 5]
[7, 1, 8]
[8, 2, 13]
[9, 2, 21]
[16, 3, 610]
[17, 3, 987]
[32, 7, 1346269]
[33, 7, 2178309]
[64, 13, 6557470319842]
[65, 14, 10610209857723]
[255, 53, 54122222371037658776676579571233761483351206693809497]
[256, 53, 87571595343018854458033386304178158174356588264390370]
[257, 54, 141693817714056513234709965875411919657707794958199867]
"#(7) 一次方程式の解の法則"
"(7-1) 実数解"
0.1
"(7-2) 分数解"
(1/10)
"(7-3) 虚数解"
(0.15+0.25i)
"(7-4) 虚数&分数解"
((3/20)+(1/4)*i)
"(7-5) 多倍長整数の行列解"
Matrix[[(3703703670366790122789/1234567890123456789890), (-2469135780244567900789/1234567890123456789890)]]
"(7-7) 多倍長整数 複素数分数の行列解"
Matrix[[((-3703703671/1234567890123456789890)-(3/1)*i), ((2469135781/1234567890123456789890)+(2/1)*i)]]