How to resolve the algorithm Continued fraction step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Continued fraction step by step in the Picat programming language

Table of Contents

Problem Statement

The task is to write a program which generates such a number and prints a real representation of it. The code should be tested by calculating and printing the square root of 2, Napier's Constant, and Pi, using the following coefficients: For the square root of 2, use

a

0

= 1

{\displaystyle a_{0}=1}

then

a

N

= 2

{\displaystyle a_{N}=2}

.

b

N

{\displaystyle b_{N}}

is always

1

{\displaystyle 1}

. For Napier's Constant, use

a

0

= 2

{\displaystyle a_{0}=2}

, then

a

N

= N

{\displaystyle a_{N}=N}

.

b

1

= 1

{\displaystyle b_{1}=1}

then

b

N

= N − 1

{\displaystyle b_{N}=N-1}

. For Pi, use

a

0

= 3

{\displaystyle a_{0}=3}

then

a

N

= 6

{\displaystyle a_{N}=6}

.

b

N

= ( 2 N − 1

)

2

{\displaystyle b_{N}=(2N-1)^{2}}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Continued fraction step by step in the Picat programming language

Source code in the picat programming language

go =>

  % square root 2
  continued_fraction(200, sqrt_2_ab, V1),
  printf("sqrt(2) = %w (diff: %0.15f)\n", V1, V1-sqrt(2)),

  % napier
  continued_fraction(200, napier_ab, V2),
  printf("e       = %w (diff: %0.15f)\n", V2, V2-math.e),

  % pi
  continued_fraction(200, pi_ab, V3),
  printf("pi      = %w (diff: %0.15f)\n", V3, V3-math.pi),
  % get a better precision
  continued_fraction(20000, pi_ab, V3b),
  printf("pi      = %w (diff: %0.15f)\n", V3b, V3b-math.pi),
  nl.

continued_fraction(N, Compute_ab, V) ?=>
  continued_fraction(N,  Compute_ab, 0, V).
 
continued_fraction(0,  Compute_ab, Temp, V) ?=>
  call(Compute_ab, 0, A, _),
  V = A + Temp.
 
continued_fraction(N, Compute_ab, Tmp, V) =>
  call(Compute_ab, N, A, B),
  Tmp1 = B / (A + Tmp),
  N1 = N - 1,
  continued_fraction(N1, Compute_ab, Tmp1, V).

% definitions for square root of 2
sqrt_2_ab(0, 1, 1).
sqrt_2_ab(_, 2, 1).
 
% definitions for napier
napier_ab(0, 2, _).
napier_ab(1, 1, 1).
napier_ab(N, N, V) :-
	V is N - 1.
 
% definitions for pi
pi_ab(0, 3, _).
pi_ab(N, 6, V) :-
	V is (2 * N - 1)*(2 * N - 1).

continued_fraction_it(Fun, N) = Ret =>
  Temp = 0.0,
  foreach(I in N..-1..1)
     [A,B] = apply(Fun,I),
     Temp := B / (A + Temp)
  end,
  F = apply(Fun,0),
  Ret = F[1] + Temp.

fsqrt2(N)  = [cond(N > 0, 2, 1),1].
fnapier(N) = [cond(N > 0, N,2), cond(N>1,N-1,1)].
fpi(N)     = [cond(N>0,6,3), (2*N-1) ** 2].

  

You may also check:How to resolve the algorithm Emirp primes step by step in the Groovy programming language
You may also check:How to resolve the algorithm Define a primitive data type step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Raku programming language
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the C programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Phixmonti programming language