javascript - How to create specific chain of numbers -


i have school prompt create function finds out integer, in range user enters, produces longest chain. "chain" consists of:

  • ask user number.
  • if number even, divide 2.
  • if it's odd multiply 3 , add 1.
  • continue until reach 1.

e.g. if start 3, sequence 3-10-5-16-8-4-2-1. function print out number produces longest chain , actual integers in chain (length of chain).

this have far. can't figure out.

function chain2()         var startnumber = prompt("enter starting number of range", "4");         var endnumber = prompt ("enter ending number of range", "9")          var separator="-";         while (currentnumber>1){             if (currentnumber%2==1){                 currentnumber=currentnumber*3+1;                 chain+=separator+currentnumber;             }             else if (currentnumber%2==0){                 currentnumber=currentnumber/2;                 chain+=separator+currentnumber;             }          }         return chain;         }         document.write(chain()); 

as homework assignment, should nudge rather give answer. if there's single point of advice give you, it's should separate work easy-to-understand functions first. writing 1 big function recipe disaster. function have right now

  • gives 2 prompts user input
  • loops based on user inputs
  • determines , odd values
  • performs basic arithmetic according collatz chain procedure

breaking separate parts make easier reason code. start top-level function wish work, , work way down there.

function findmaxcollatzinrange(x,y) {   // max = ...   // ...   return max; } 

if find needing other functions, that's normal. write them need them.

your basic algorithm should this:

  • for numbers between min , max (inclusive) ...
  • compute , record collatz chain length each number
  • based on recorded lengths, return number highest length

so here's how this. way beyond scope of javascript homework assignment, found fun work on.

i'm ok posting code because know can't directly copy/pasted answer assignment. hope there things here encourage explore of techniques used.

// compute collatz chain given number, x const collatz = y (f=> y=> x=> {   if (x === 1)   return append (x) (y);   if (iseven(x)) return f (append (x) (y)) (x/2);   else           return f (append (x) (y)) (x*3 + 1); }) ([]);  // compute length of collatz chain const collatzlen = comp (len) (collatz);  // create range minimum, n, maximum, m const range = y (f=> r=> n=> m=> {   if (n > m)  return r;   else        return f (append (n) (r)) (n+1) (m); }) ([]);  // compute maximum collatz chain length given range const maxcollatz = (n,m) =>   comp (foldl (([x,xlen])=> ([y,ylen])=> ylen > xlen ? [y,ylen] : [x,xlen]) ([0,1]))        (map (n=> [n,collatzlen (n)]))        (range (n) (m)); 

here's sample output of functions can see what's going on

collatz (3) //=> [ 3, 10, 5, 16, 8, 4, 2, 1 ]  collatzlen (3) //=> 8  range (3) (10) //=> [ 3, 4, 5, 6, 7, 8, 9, 10 ]  maxcollatz (3,10) //=> [ 9, 20 ] 

the way read last output [ 9, 20 ] is, "the number 9 has longest collatz chain length, length of 20"

simply put, each function responsible doing one thing. function answers problem maxcollatz function, computes list of chain lengths using collatzlen each element in range. lastly, list reduced determine number has longest chain.

here generic dependencies

const comp = f=> g=> x=> f (g (x)); const mod = x=> y=> y % x; const eq = x=> y=> y === x; const iseven = comp (eq (0)) (mod (2)); const prop = x=> y=> y [x]; const len = prop ('length'); const concat = x=> xs=> xs .concat (x); const append = x=> concat ([x]); const foldl = f=> y=> xs=> xs .reduce ((x,y)=> f (x) (y), y); const map = f=> xs=> xs .map (x=> f(x));  // u-combinator const u = f=> f (f);  // y-combinator const y = u (h=> f=> f (x=> h (h) (f) (x))); 

additional reading:


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -