3 Replies - 734 Views - Last Post: 26 September 2012 - 04:38 AM

#1 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Exam Question

Posted 25 September 2012 - 11:36 AM

Ok, so I was looking at some exam problems that are given in order to get into a certain college. And I could solve them all but one , you can consider this a challenge if you like (please use spoiler tags) . Also, it requires only the algorithm so you should provide it in pseudocode.

Data: You are the following sequence of numbers 1,2,2,3,3,3,4,4,4,4,... Basically each number repeats itself accordingly to its value .
Requirement: Provide an algorithm that will return the Nth element of the sequence ( N will be given by input ) . The algorithm must run in a constant time ( it should not depend on the value of N )

I tried some wild ideas but it didn't work so far , so please have a try. Enjoy solving !

Is This A Good Question/Topic? 0
  • +

Replies To: Exam Question

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8938
  • View blog
  • Posts: 33,504
  • Joined: 12-June 08

Re: Exam Question

Posted 25 September 2012 - 12:26 PM

Interesting way to crowd source an exam answer. Have you thought about just two for loops and a counter?
Was This Post Helpful? 0
  • +
  • -

#3 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 2,999
  • Joined: 12-January 10

Re: Exam Question

Posted 25 September 2012 - 12:27 PM

put the thing in a loop and have the number become the increment variable. then also have the number in the loop to print the variable as many times as the loop runs.
Was This Post Helpful? 0
  • +
  • -

#4 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Exam Question

Posted 26 September 2012 - 04:38 AM

Actually the solution looks like this .

Since the numbers go like 1, 2, 2, 3, 3, 3, 4, 4, 4, 4,etc.
We can write n like this:

x(x+1) / 2 = n then we find out how much x is and so we'll have an = x if x is integer and an = [x] + 1 if x is float .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1