Results 1 to 20 of 27
Thread: While condition/math series.
 10312011, 02:36 PM #1Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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:
Java 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(); } }
1
2 3
4 5 6
7 8 9 10
< Yea it's keep making new lines.
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(n1) equals to a(n2) && a(n3) && a(n4) and so on n times.
an = (an1) +1
else
an = a(n1);
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(n1) (3) now we ask if a(n1)(3) == a(n2)(3) && a(n2)(3) == a(n3)(3) see, we ask 3 times cause a(n1) 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.Last edited by tnrh1; 11012011 at 03:36 PM.
 10312011, 07:01 PM #2Member
 Join Date
 Oct 2011
 Posts
 92
 Rep Power
 0
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 x1 equals to x2 && x3 && x4 and so on x times.
x = (x1) +1
else
x = x1;
That is mathematically void. if x1 equals x2... 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?
 10312011, 07:25 PM #3Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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 reexplain 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 (n1) +1.
In case this is trueif x1 equals to x2 && x3 && x4 and so on x times.
When the values comes back from the basic case the (n1) 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.
 10312011, 11:41 PM #4Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
Re: While condition/math series.
Really? no one?
This is definitely a hard challenge, hope we can solve it together.
 11012011, 12:48 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
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 11th number was 66 and the 12th number was 78. Then, clearly 70 appears on the 12th 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.
 11012011, 08:55 AM #6Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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? :)
 11012011, 09:21 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
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:
Java 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(); } } }
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 11012011, 11:49 AM #8Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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:
Java 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(); }
I'm starting to think that there is no a mathematical formula for this series.
 11012011, 11:55 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
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,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 11012011, 12:12 PM #10Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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.
 11012011, 01:27 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
 11012011, 03:00 PM #12Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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
 11012011, 03:21 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
Re: While condition/math series.
I have the stamina of a seal; I lie on the beach instead of running on it.
 11022011, 04:40 AM #14Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
Re: While condition/math series.
Note that 1+2+3+ ... +n == n*(n+1)/2
Not only is it a rectangle it is very nearly a square. So, ...
Given a number x, it will appear on line n where
(n1)*n/2 + 1 <= x <= n*(n+1)/2
A very little algebra gives
n(n1) + 2 <= 2x <= n(n+1)
But n(n1)+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.
 11022011, 04:47 PM #15Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
Re: While condition/math series.
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(n1)+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.
Again, the goal is to find the formula that will calculate the line number of 'n' .
 11022011, 05:35 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
Re: While condition/math series.
I have the stamina of a seal; I lie on the beach instead of running on it.
 11022011, 09:40 PM #17Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
Re: While condition/math series.
the goal is to find the formula that will calculate the line number of 'n'
 11022011, 09:49 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
 11032011, 01:23 AM #19Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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
 11032011, 02:56 AM #20Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
Re: While condition/math series.
But it's better then nothing

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.)
Similar Threads

Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12072010, 07:23 AM 
Perrin series
By moamen in forum New To JavaReplies: 6Last Post: 12042009, 06:27 PM 
Help with summing series
By xplsivo in forum New To JavaReplies: 8Last Post: 11232009, 08:37 PM 
IFCondition in a String
By lenaz in forum Advanced JavaReplies: 1Last Post: 07182009, 12:07 PM 
How to add a second series in jfreechart
By Manfizy in forum New To JavaReplies: 1Last Post: 03232009, 12:16 PM
Bookmarks