Results 1 to 20 of 27
Thread: While condition/math series.
- 10-31-2011, 01:36 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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:
For x=10; the output would be: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(); } }
Then I looked again to see the problems and I find out that the first loop runs much more then it should be.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(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.Last edited by tnrh1; 11-01-2011 at 02:36 PM.
- 10-31-2011, 06:01 PM #2
Member
- 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 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, 06:25 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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 trueelse you send (n-1).if x-1 equals to x-2 && x-3 && x-4 and so on x times.
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, 10:41 PM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: While condition/math series.
Really? no one?

This is definitely a hard challenge, hope we can solve it together.
- 10-31-2011, 11:48 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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, 07:55 AM #6
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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, 08:21 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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(); } } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-01-2011, 10:49 AM #8
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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:
But again, I'm interested on the second question.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.
- 11-01-2011, 10:55 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-01-2011, 11:12 AM #10
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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, 12:27 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
- 11-01-2011, 02:00 PM #12
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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, 02:21 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: While condition/math series.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-02-2011, 03:40 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: While condition/math series.
n*(n+1) is the rectangle I spoke about in #5.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
(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, 03:47 PM #15
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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.
I guess the answer if hide in here but I just can't understand it, I lost you after the dot.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.
Again, the goal is to find the formula that will calculate the line number of 'n' .
- 11-02-2011, 04:35 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: While condition/math series.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-02-2011, 08:40 PM #17
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: While condition/math series.
It is Math.round(Math.sqrt(2*n))the goal is to find the formula that will calculate the line number of 'n'
- 11-02-2011, 08:49 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
- 11-03-2011, 12:23 AM #19
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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, 01:56 AM #20
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: While condition/math series.
You're welcome.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: 12-07-2010, 06:23 AM -
Perrin series
By moamen in forum New To JavaReplies: 6Last Post: 12-04-2009, 05:27 PM -
Help with summing series
By xplsivo in forum New To JavaReplies: 8Last Post: 11-23-2009, 07:37 PM -
IF-Condition in a String
By lenaz in forum Advanced JavaReplies: 1Last Post: 07-18-2009, 12:07 PM -
How to add a second series in jfreechart
By Manfizy in forum New To JavaReplies: 1Last Post: 03-23-2009, 11:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks