# While condition/math series.

Printable View

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-31-2011, 02:36 PM
tnrh1
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:

1
2 3
4 5 6
7 8 9 10

I thought, oh good that easy I started coding and that's the result:

Code:

```public static void Ex1(int x)         {             int counter = 1;             for (int i = 1; i <= x; i++)             {                 for (int j = 1; j <= i; j++)                 {                     if (counter <= x)                     {                         System.out.print(counter+" ");                             counter++;                     }                      }                 System.out.println();             }         }```
For x=10; the output would be:

Quote:

1
2 3
4 5 6
7 8 9 10

<--- Yea it's keep making new lines.

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 !!!!!!!

Thanks in advanced.
• 10-31-2011, 07:01 PM
christopherx
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:

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

That is mathematically void. if x-1 equals x-2... x = (x- 1) + 1? That just means x = x. Is it perhaps because you aren't adjusting your value that you're getting these problems?
• 10-31-2011, 07:25 PM
tnrh1
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
Quote:

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.
• 10-31-2011, 11:41 PM
tnrh1
Re: While condition/math series.
Really? no one? :(-:
This is definitely a hard challenge, hope we can solve it together.
• 11-01-2011, 12:48 AM
pbrockway2
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.
• 11-01-2011, 08:55 AM
tnrh1
Re: While condition/math series.
There is a certain ratio between each number that I don't know :( yea I try to find the formula.
Maybe the experts in here mind help me? :)
• 11-01-2011, 09:21 AM
JosAH
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:

Code:

```public class T {         public static void main(String[] args) {                 int max= 25; // just an example                                 outer:                 for (int number= 1, line= 1;; line++) {                         for (int column= 1; column <= line; column++) {                                 System.out.print(number++ +" ");                                 if (--max == 0) break outer;                         }                         System.out.println();                 }         } }```
kind regards,

Jos
• 11-01-2011, 11:49 AM
tnrh1
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 6
7 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.

You can solve it like this:
Code:

```public static void Ex1(int x)         {             int counter = 1;             for (int i = 1; i <= x; i++)             {                 for (int j = 1; j <= i; j++)                 {                     if (counter <= x)                     {                         System.out.print(counter+" ");                             counter++;                     }                     }                 if(counter<=x)                         System.out.println();             }```
But again, I'm interested on the second question.
I'm starting to think that there is no a mathematical formula for this series.
• 11-01-2011, 11:55 AM
JosAH
Re: While condition/math series.
Note that 1+2+3+ ... +n == n*(n+1)/2; it is the base of the formula you're looking for.

kind regards,

Jos
• 11-01-2011, 12:12 PM
tnrh1
Re: While condition/math series.
Series formulas shouldn't have 2 parameters?a & n?
a1 = 1
a2 = 2
a3 = 2
a4 = 4
and so on ..

so a*n = ?????
Sorry I don't get you, I really appreciate your help.
• 11-01-2011, 01:27 PM
JosAH
Re: While condition/math series.
Quote:

Originally Posted by tnrh1
Series formulas shouldn't have 2 parameters?a & n?

That is just a matter of notation; as I wrote above: the series I described (and its closed form) is the key to your problem.

kind regards,

Jos
• 11-01-2011, 03:00 PM
tnrh1
Re: While condition/math series.
I've played with the formula alittle bit,
n = the value in the series (line in this case) and it returns the highest number that can be placed in this line.

1
2 3
4 5 6
7 8 9 10

In case n = 3 that should return 6.
n = an*(an+1)/2
n = 3*(3+1)/2
n = 3*(4)/2
n = 3*2
n = 6

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!

Factorial - Wikipedia, the free encyclopedia
• 11-01-2011, 03:21 PM
JosAH
Re: While condition/math series.
Quote:

Originally Posted by tnrh1
I've played with the formula alittle bit,
n = the value in the series (line in this case) and it returns the highest number that can be placed in this line.

1
2 3
4 5 6
7 8 9 10

In case n = 3 that should return 6.

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
• 11-02-2011, 04:40 AM
pbrockway2
Re: While condition/math series.
Quote:

Note that 1+2+3+ ... +n == n*(n+1)/2
n*(n+1) is the rectangle I spoke about in #5.

Not only is it a rectangle it is very nearly a square. So, ...

Given a number x, it will appear on line n where

(n-1)*n/2 + 1 <= x <= n*(n+1)/2

A very little algebra gives

n(n-1) + 2 <= 2x <= n(n+1)

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.
• 11-02-2011, 04:47 PM
tnrh1
Re: While condition/math series.
Quote:

Originally Posted by JosAH
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.

Quote:

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' .
• 11-02-2011, 05:35 PM
JosAH
Re: While condition/math series.
Quote:

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

You already have the answer: for line n, the first number on that line is n*(n+1)/2-n+1 and the last number is n*(n+1)/2, for a number p the following relation holds: n*(n+1)/2-n+1 <= p <= n*(n+1)/2.

kind regards,

Jos
• 11-02-2011, 09:40 PM
pbrockway2
Re: While condition/math series.
Quote:

the goal is to find the formula that will calculate the line number of 'n'
It is Math.round(Math.sqrt(2*n))
• 11-02-2011, 09:49 PM
JosAH
Re: While condition/math series.
Quote:

Originally Posted by pbrockway2
It is Math.round(Math.sqrt(2*n))

Proof! We demand proof! And we don't buy "because it is so"! ;-)

kind regards,

Jos
• 11-03-2011, 01:23 AM
tnrh1
Re: While condition/math series.
The solution is not worth a thing without the way - orginally said by my teacher ^^
But it's better then nothing :P I will test it now
• 11-03-2011, 02:56 AM
pbrockway2
Re: While condition/math series.
Quote:

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.)
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last