Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,757 members, 7,824,175 topics. Date: Saturday, 11 May 2024 at 02:56 AM

[PROGRAMMERS] Work It Out- On Bumpy Sequence - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / [PROGRAMMERS] Work It Out- On Bumpy Sequence (1947 Views)

Why Programmers Work At Night / Programming Contests: Sequence / Interaction Diagrams - Sequence And Collaboration In Object Oriented Software D (2) (3) (4)

(1) (Reply) (Go Down)

[PROGRAMMERS] Work It Out- On Bumpy Sequence by prodam(m): 2:35am On Mar 01, 2013
Hello Geek,

The section has been kinda unusually dull for some times now..

lets try this: (though I have the solution already in Java), but the Time and Efficiency of the program is what we are going to be looking into.
Taking Algorithm, library Functions used and all that into consideration.

THE PROBLEM:

Start with any positive number n. If n is even divide it by 2, if n is odd multiply by 3 and add 1. Repeat until n becomes 1. The sequence that is generated is called a bumpy sequence. For example, the bumpy sequence that starts at n = 10 is:
10, 5, 16, 8, 4, 2, 1
and the one that starts at 11 is
11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
Create a method with the signature int lengthBumpy(int n)

such that lengthBumpy(n)returns the number of elements in the sequence

For example: lengthBumpy(10) returns 7, and lengthBumpy(11) returns 15.
You may assume that the argument n is positive and that the sequence converges to 1 without arithmetic overflow.

You can write your solutions in any language
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by Nobody: 7:57am On Mar 01, 2013
function BumpyLength(number){
while(number != 1){
if((number % 2) == 0){
number=number / 2;
}else{
number=(number * 3) + 1;
}
console.log("%s,",number);
}
}

JavaScript Version
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by Nobody: 8:34am On Mar 01, 2013
had issues using recursive functions. i guess Js hates that
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by Fayimora(m): 3:17am On Mar 02, 2013
This is called The 3N+1 problem.

I wrote this in Haskell but I was pretty sure the code looked too cryptic so I re-wrote it in Scala.

Code: https://gist.github.com/fayimora/5069397

The gist also contains an HTML file I generated(see screenshot below), you can download it and open in your browser cool

I can explain the code in detail if anyone wants me to but I can't be arsed to right now grin



@pc_guru You can implement it recursively in JS easily. Use a nested function!
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by prodam(m): 8:29am On Mar 02, 2013
@pc guru..very concise snippet there..but the output will be the sequence and not the length..
I mean console.log() would only print the numbers to the console(probably meant for debugging purpose)

@fayimora!..scala?..am lost..30lines??..pretty much!..explanation pls @ ur leisure..
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by Nobody: 10:17am On Mar 02, 2013
Sorry was in class when i did this, didn't read through,
function BumpyLength(number){
var _array=[];
var addOne=function(ar){
return (1 + ar.length);
}
while(number != 1){
if((number % 2) == 0){
number=number / 2;
_array.push(number);
}else{
number=(number * 3) + 1;
_array.push(number);
}

}
console.log("The Length of array is %s,",addOne(_array));
}

@Fayiomora so cool you also use Sublime on Ubuntu,that's python right ?
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by codeaddict(m): 2:37pm On Mar 02, 2013
Missing Python.
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by Fayimora(m): 6:26pm On Mar 02, 2013
@prodam the code there is 30 lines but look at it closely. The main stuff is about 10 lines. I added a few print statements for those who haven't used Scala wink Also, it's a recursive solution. I can make this way shorter but trust me, the code will be really cryptic if you are yet to meet FP.

@pc_guru That is vim on a Mac. I'm a vim user. Also, the language is Scala not Python wink
Also why don't you remove `addOne` and just print `_array.length+1` ?
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by Nobody: 7:07pm On Mar 02, 2013
i thought it was way way easier to read, oh you use vim ? mehn guy my hat goes off to you, last time i did, i almost killed someone, oops i could have sworn it was python.
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by prodam(m): 9:16pm On Mar 02, 2013
@pc guru..yeah!..good one there bro..*though javascrpt iz a cheat*
*even checked ur site and was like wow!..especially texts about the YII framework.u v gone far bro..
itz jst that i dnt lyk php..maybe later.

@fayimora..alright!..u're not sounding/coding lyk a typical naija programmer at all.gess u're nt based here in my fatherland..I love the vim on mac and the "uncommon" programming languages u code..but I can't really spend time learning those stuffz now.. cos
here in Naija, the economy/market decides what technology we learn and use.

lets see what some peeps gat abt this stuff in C or any other lang without making use of some 'sophisticated' data structure technique lyk arraylist, stringdictionary, and all that instead applying recursion to generate the sequence..thatz exactly what fayimora did..
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by Fayimora(m): 2:20am On Mar 03, 2013
@pc_guru yeah vim is AWESOME.. Best text editor smiley Learning vim is like learning Functional Programming. To learn FP, you have to forget all you know about programming. To learn vim, you have to forget all you know about text editing first.

lol @ "uncommon languages I code in".. I use the best tool for the job wink
Re: [PROGRAMMERS] Work It Out- On Bumpy Sequence by prodam(m): 4:46pm On Mar 03, 2013
Alright!..
Its all good.

(1) (Reply)

Ole! Catch The Thief! (destination: UNILAG)... Early Stages Of Development Video / Looking For A C# MVC Developer With Experience In Developing An Accounting App / Virus Programming

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.