How to resolve the algorithm Stern-Brocot sequence step by step in the Mathematica / Wolfram Language programming language
Published on 22 June 2024 08:30 PM
How to resolve the algorithm Stern-Brocot sequence step by step in the Mathematica / Wolfram Language programming language
Table of Contents
Problem Statement
For this task, the Stern-Brocot sequence is to be generated by an algorithm similar to that employed in generating the Fibonacci sequence.
Show your output on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Stern-Brocot sequence step by step in the Mathematica / Wolfram Language programming language
Wolfram Programming Language Code:
sb = {1, 1};
Do[
sb = sb~Join~{Total@sb[[i - 1 ;; i]], sb[[i]]}
,
{i, 2, 1000}
]
Take[sb, 15]
Flatten[FirstPosition[sb, #] & /@ Range[10]]
First@FirstPosition[sb, 100]
AllTrue[Partition[Take[sb, 1000], 2, 1], Apply[GCD] /* EqualTo[1]]
Explanation:
-
Fibonacci Sequence:
- Initialize
sb
with the first two Fibonacci numbers,1
and1
. - Use a
Do
loop to iteratively calculate the next Fibonacci numbers by summing up the previous two numbers and appending them tosb
.
- Initialize
-
Printing First 15 Fibonacci Numbers:
Take[sb, 15]
prints the first 15 Fibonacci numbers in the listsb
.
-
Positions of First 10 Fibonacci Numbers:
Flatten[FirstPosition[sb, #] & /@ Range[10]]
finds the positions of the first 10 Fibonacci numbers (from 1 to 10) in the listsb
and flattens the result.
-
Position of Fibonacci Number 100:
First@FirstPosition[sb, 100]
finds the first occurrence of the Fibonacci number 100 in the listsb
.
-
GCD of Consecutive Fibonacci Numbers:
AllTrue[Partition[Take[sb, 1000], 2, 1], Apply[GCD] /* EqualTo[1]]
checks if the greatest common divisor (GCD) of all pairs of consecutive Fibonacci numbers in the first 1000 terms is 1. If this condition is true for all pairs, it returnsTrue
, otherwiseFalse
.
Source code in the wolfram programming language
sb = {1, 1};
Do[
sb = sb~Join~{Total@sb[[i - 1 ;; i]], sb[[i]]}
,
{i, 2, 1000}
]
Take[sb, 15]
Flatten[FirstPosition[sb, #] & /@ Range[10]]
First@FirstPosition[sb, 100]
AllTrue[Partition[Take[sb, 1000], 2, 1], Apply[GCD] /* EqualTo[1]]
You may also check:How to resolve the algorithm Lychrel numbers step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the 11l programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Pathological floating point problems step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Scheme programming language