Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Content deleted Content added
Line 1,436: | Line 1,436: | ||
3141592653589793 / 1000000000000000 : 3 7 15 1 292 1 1 1 2 1 3 1 14 4 2 3 1 12 5 1 5 20 1 11 1 1 1 2 ok</pre> |
3141592653589793 / 1000000000000000 : 3 7 15 1 292 1 1 1 2 1 3 1 14 4 2 3 1 12 5 1 5 20 1 11 1 1 1 2 ok</pre> |
||
=={{header|Fortran}}== |
|||
<syntaxhighlight lang="f90"> |
|||
program r2cf_demo |
|||
implicit none |
|||
call write_r2cf (1, 2) |
|||
call write_r2cf (3, 1) |
|||
call write_r2cf (23, 8) |
|||
call write_r2cf (13, 11) |
|||
call write_r2cf (22, 7) |
|||
call write_r2cf (-151, 77) |
|||
call write_r2cf (14142, 10000) |
|||
call write_r2cf (141421, 100000) |
|||
call write_r2cf (1414214, 1000000) |
|||
call write_r2cf (14142136, 10000000) |
|||
call write_r2cf (31, 10) |
|||
call write_r2cf (314, 100) |
|||
call write_r2cf (3142, 1000) |
|||
call write_r2cf (31428, 10000) |
|||
call write_r2cf (314285, 100000) |
|||
call write_r2cf (3142857, 1000000) |
|||
call write_r2cf (31428571, 10000000) |
|||
call write_r2cf (314285714, 100000000) |
|||
contains |
|||
! This implementation of r2cf both modifies its arguments and |
|||
! returns a value. |
|||
function r2cf (N1, N2) result (q) |
|||
integer, intent(inout) :: N1, N2 |
|||
integer :: q |
|||
integer r |
|||
! We will use floor division, where the quotient is rounded |
|||
! towards negative infinity. Whenever the divisor is positive, |
|||
! this type of division gives a non-negative remainder. |
|||
r = modulo (N1, N2) |
|||
q = (N1 - r) / N2 |
|||
N1 = N2 |
|||
N2 = r |
|||
end function r2cf |
|||
subroutine write_r2cf (N1, N2) |
|||
integer, intent(in) :: N1, N2 |
|||
integer :: digit, M1, M2 |
|||
character(len = :), allocatable :: sep |
|||
write (*, '(I0, "/", I0, " => ")', advance = "no") N1, N2 |
|||
M1 = N1 |
|||
M2 = N2 |
|||
sep = "[" |
|||
do while (M2 /= 0) |
|||
digit = r2cf (M1, M2) |
|||
write (*, '(A, I0)', advance = "no") sep, digit |
|||
if (sep == "[") then |
|||
sep = "; " |
|||
else |
|||
sep = ", " |
|||
end if |
|||
end do |
|||
write (*, '("]")', advance = "yes") |
|||
end subroutine write_r2cf |
|||
end program r2cf_demo |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>$ gfortran -std=f2018 continued-fraction-from-rational.f90 && ./a.out |
|||
1/2 => [0; 2] |
|||
3/1 => [3] |
|||
23/8 => [2; 1, 7] |
|||
13/11 => [1; 5, 2] |
|||
22/7 => [3; 7] |
|||
-151/77 => [-2; 25, 1, 2] |
|||
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29] |
|||
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2] |
|||
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12] |
|||
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2] |
|||
31/10 => [3; 10] |
|||
314/100 => [3; 7, 7] |
|||
3142/1000 => [3; 7, 23, 1, 2] |
|||
31428/10000 => [3; 7, 357] |
|||
314285/100000 => [3; 7, 2857] |
|||
3142857/1000000 => [3; 7, 142857] |
|||
31428571/10000000 => [3; 7, 476190, 3] |
|||
314285714/100000000 => [3; 7, 7142857]</pre> |
|||