# Exam Question

Page 1 of 1

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

### #1 TwoOfDiamonds

Reputation: 38
• Posts: 200
• 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

• Suitor #2

Reputation: 6463
• Posts: 23,500
• 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?

### #3 DarenR

• D.I.C Lover

Reputation: 200
• Posts: 1,534
• 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.

### #4 TwoOfDiamonds

Reputation: 38
• Posts: 200
• 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 .