Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,538 members, 7,808,984 topics. Date: Thursday, 25 April 2024 at 08:29 PM

I Hereby Conjure All Programming Wizards On This Forum To This Coding Challenge - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / I Hereby Conjure All Programming Wizards On This Forum To This Coding Challenge (1338 Views)

Mini Web Application Coding Challenge For Programmers / Java Coding Challenge: Task Scheduler / Coding Challenge 5: Substring Generator (2) (3) (4)

(1) (Reply) (Go Down)

I Hereby Conjure All Programming Wizards On This Forum To This Coding Challenge by tushutt(m): 2:42pm On Aug 12, 2011
The following iterative sequence is defined for the set of positive integers:

n n/2 (n is even)
n 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 40 20 10 5 16 8 4 2 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms.[b] it is thought that al[/b]l starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.
Re: I Hereby Conjure All Programming Wizards On This Forum To This Coding Challenge by werepeLeri: 3:14pm On Aug 12, 2011
var startOfLongest = 0U;
var longestChain = 0U;

for (var start = 1000000U; start > 0; start--)
{
var s = start;
var length = 1U;

while (s != 1)
{
if (s % 2 == 0)
s /= 2;
else
s = 3 * s + 1;
length++;
}

if (length < longestChain)
continue;

startOfLongest = start;
longestChain = length;
}

var answer = startOfLongest;
Re: I Hereby Conjure All Programming Wizards On This Forum To This Coding Challenge by Nobody: 6:19pm On Aug 12, 2011

Re: I Hereby Conjure All Programming Wizards On This Forum To This Coding Challenge by Fayimora(m): 6:25pm On Aug 12, 2011
Hmmm lets make it a bit interesting. Thats way too easy. Running the 3n+1 game on 13 produces 10 numbers. Those 10 numbers are referred to the cycle length of 13.

OK now back to the question, let i and j be 2 numbers on a line.

The Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
You can assume that no operation overflows a 32-bit integer.

The Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

So if you have input as;
1 10
100 200

the output should be;
1 10 20
100 200 125


Now thats betta, I expect the OP to be the first to solve it. cheesy

(1) (Reply)

URGENT: Advance School Management Software Needed / Please Help A Script Kiddie Out With Javascript / To Whom It May Concern.

(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. 11
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.