How to resolve the algorithm Recaman's sequence step by step in the VBScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Recaman's sequence step by step in the VBScript programming language

Table of Contents

Problem Statement

The Recamán's sequence generates Natural numbers. Starting from a(0)=0, the n'th term a(n), where n>0, is the previous term minus n i.e a(n) = a(n-1) - n but only if this is both positive and has not been previousely generated. If the conditions don't hold then a(n) = a(n-1) + n.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Recaman's sequence step by step in the VBScript programming language

Source code in the vbscript programming language

' Recaman's sequence - vbscript - 04/08/2015
	nx=15
	h=1000
	Wscript.StdOut.WriteLine "Recaman's sequence for the first " & nx & " numbers:"
	Wscript.StdOut.WriteLine recaman("seq",nx)
	Wscript.StdOut.WriteLine "The first duplicate number is: " & recaman("firstdup",0)
	Wscript.StdOut.WriteLine "The number of terms to complete the range 0--->"& h &" is: "& recaman("numterm",h)
	Wscript.StdOut.Write vbCrlf&".../...": zz=Wscript.StdIn.ReadLine()
	
function recaman(op,nn)
	Dim b,d,h
	Set b = CreateObject("Scripting.Dictionary")
	Set d = CreateObject("Scripting.Dictionary")
    list="0" : firstdup=0
	if op="firstdup" then
		nn=1000 : firstdup=1
	end if
	if op="numterm" then
		h=nn : nn=10000000 : numterm=1
	end if
	ax=0  'a(0)=0
	b.Add 0,1  'b(0)=1
	s=0
	for n=1 to nn-1
        an=ax-n
		if an<=0 then 
			an=ax+n
		elseif b.Exists(an) then 
			an=ax+n
		end if
		ax=an  'a(n)=an
		if not b.Exists(an) then b.Add an,1  'b(an)=1
		if op="seq" then
			list=list&" "&an
		end if
		if firstdup then
			if d.Exists(an) then
				recaman="a("&n&")="&an
				exit function
			else
				d.Add an,1  'd(an)=1
			end if
		end if
		if numterm then
			if an<=h then
				if not d.Exists(an) then
					s=s+1
					d.Add an,1  'd(an)=1
				end if
				if s>=h then
					recaman=n
					exit function
				end if
			end if
		end if
	next 'n
	recaman=list
end function 'recaman

  

You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Python programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the QuickBASIC 4.5 programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Tcl programming language
You may also check:How to resolve the algorithm Empty program step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Soundex step by step in the MUMPS programming language