While condition/math series.

Hello everyone,
few days ago someone asked for with the following Exercice:
Enter a non negative number and the and the prorgam will print all the number from 0 to the input in this order.
Let's assume that we entered 10, the output would be:

Then I looked again to see the problems and I find out that the first loop runs much more then it should be.
for x=10 it should run 4 times:
1. 1
2. 2 3
3. 4 5 6
4. 7 8 9 10
and for x=20 it should run 6 times.

Now I wonder how to make a method return the lines need for a certain number.

The series would look like that: 1,2,2,3,3,3,4,4,4,4,5,5,5,5,5 and so on.
There is a certain legal I can not find.
As far as I know all the solutions of those question are based on math, I think I know the answer but it's defentlly not math:

if a(n-1) equals to a(n-2) && a(n-3) && a(n-4) and so on n times.
an = (an-1) +1
else
an = a(n-1);

Let me show you what I mean I know it's confusing:
1,2,2,3,3,3,4,4,4,4,
Let's say n=7, the 7 number on the series is 4.
We go to a(n-1) (3) now we ask if a(n-1)(3) == a(n-2)(3) && a(n-2)(3) == a(n-3)(3) see, we ask 3 times cause a(n-1) is 3.
then a7=4.

But this is definitely not Math .. each series got a formula.

Anyway, please help me! I'm trying to solve this for days that's so hard !!!!!!!

Re: While condition/math series.

Right. Well the problem got rather obscured. you just wanna eliminate those extra lines on the end? Or you want a method that returns the corresponding line for a number? I'm not really sure what you mean but:

Re: While condition/math series.

I want to calculate the value of the first loop condition.
I'm sorry for not beeing clear enough let me re-explain my self.
The user enter a number and the method returns the value in this index.
For example: input = 3; return 1,2,2,3,3,3,
The method of course will use recursion and will stop in the basic case, and return 1.

When you send the method again in recursion you have to use the previous number in the series.
So you send (n-1) +1.
In case this is true

if x-1 equals to x-2 && x-3 && x-4 and so on x times.

else you send (n-1).
When the values comes back from the basic case the (n-1) is turn into a number and the +1 is added to him.

But again, in math there is not such thing if.
There should be a formula for each series, and when you place a number in it, you will get the value in the series of the number you choosed.

Re: While condition/math series.

Re: While condition/math series.

Are you asking "what line of the output will n occur on?". Like 3 is on line 2, but what line will 70 occur on?

If so, one place you can start is with the numbers at the end of the rows (1, 3, 6, 10, ...) If you had a formula for these you would be close. Suppose you knew (somehow) that the 11-th number was 66 and the 12-th number was 78. Then, clearly 70 appears on the 12-th line.

It turns out that these numbers (1, 3, 6, 10, ...) are called the "triangular numbers", and they have a simple pattern. Rather than googling it, you might want to discover it. If you take two copies of the triangle pattern and put them together you can arrange them in an easy to count rectangular pattern.

Re: While condition/math series.

I think (imho) you're all overcomplicating it; let max be the total numbers to be printed and let line be a line number 1, 2, 3 ... so, on line c, there must be c numbers printed unless c > max. The following code snippet does it all:

Re: While condition/math series.

Jos my question is not how to print those numbers in the way I mentioned.
My question is how to do a math formula or a method (Both of the of course will work on the same legal) that will recieve a number and will return in which line he is found, this coincides with, what is the value of the series in that place:

Num = 7

Series:
{1,2,2,3,3,3,4,4,4,4,...}
return 4

1
2 3
4 5 67 8 9 10 <-- line 4
return 4.

As I said, the first question just led me to that, I don't really care about the first question.

Indeed, but it does the oppsite, the returned value should be the line number (the a*n).
I tryed to play with the formula but I can not isolate "an" permanently.
I got it on both sides of the equation which is not much helpfull if you don't know what an is, and of course we don't :(
Btw it reminds me the factorial, I tried to check the formulas, and maybe change some things but still .. I'm standing on 0!

But n(n-1)+2 and n(n+1) are both very nearly n^2. And this suggests that if we want to find n (the line number) given x (the value) that we have a look at Math.sqrt(2*x). Do that. Print your triangle again, but this time print Math.sqrt(2*counter) instead of ""+counter.

Re: While condition/math series.

You're almost there:if that basic formula (BF) returns the last number on a line 'n' and there are n numbers on that line then BF(n)+1-n is the first number on line 'n'. done.

kind regards,

Jos

Yea, so we got the formula to find the first or last number in each line.
It's not the main goal but it's definitely will lead us to it.

But n(n-1)+2 and n(n+1) are both very nearly n^2. And this suggests that if we want to find n (the line number) given x (the value) that we have a look at Math.sqrt(2*x). Do that. Print your triangle again, but this time print Math.sqrt(2*counter) instead of ""+counter.

I guess the answer if hide in here but I just can't understand it, I lost you after the dot.

Again, the goal is to find the formula that will calculate the line number of 'n' .

Re: While condition/math series.

But it's better then nothing

You're welcome.

-----

No proof, I'm afraid. Although post #5 shows the way.

Perhaps your teacher would prefer: The line of 'n' is given by Math.ceil((-1+Math.sqrt(8*n+1))/2)

(Take the expression for the right hand number of line x, N(x)=x*(x+1)/2. Solve for x to obtain the expression whose ceiling is taken. Typical of algebra, it gets you there, but the resulting expression is uglier than that suggested by geometry.)