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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Recaman's sequence step by step in the zkl 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 zkl programming language

Source code in the zkl programming language

fcn recamanW{  // -->iterator -->(n,a,True if a is a dup)
   Walker.tweak(fcn(rn,rp,d){
      n,p,a := rn.value, rp.value, p - n;
      if(a<=0 or d.find(a)) a+=2*n;
      d.incV(a); rp.set(a);
      return(rn.inc(),a,d[a]>1);
   }.fp(Ref(0),Ref(0),Dictionary()) )
}

print("First 15 members of Recaman's sequence: ");
recamanW().walk(15).apply("get",1).println();

n,a := recamanW().filter1("get",2);  // ie filter(a[n].dup)
println("First duplicate number in series is: a(%d) = %d".fmt(n,a));

rw,ns,n,a,dup := recamanW(),1000,0,0,0;
do{ n,a,dup=rw.next(); if(not dup and a<1000) ns-=1; }while(ns);
println("Range 0..1000 is covered by terms up to a(%,d)".fmt(n));

  

You may also check:How to resolve the algorithm Permutation test step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Levenshtein distance/Alignment step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Literals/String step by step in the M4 programming language
You may also check:How to resolve the algorithm Rosetta Code/Fix code tags step by step in the Perl programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Icon and Unicon programming language