Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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();
    	    } 
    	}
    For x=10; the output would be:

    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.
    Last edited by tnrh1; 11-01-2011 at 03:36 PM.

  2. #2
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default 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?

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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.

  4. #4
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: While condition/math series.

    Really? no one?
    This is definitely a hard challenge, hope we can solve it together.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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.

  6. #6
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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? :)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default 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();
    		} 
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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();
    	    }
    But again, I'm interested on the second question.
    I'm starting to think that there is no a mathematical formula for this series.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: While condition/math series.

    Quote Originally Posted by tnrh1 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: While condition/math series.

    Quote Originally Posted by tnrh1 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: While condition/math series.

    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.

  15. #15
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: While condition/math series.

    Quote Originally Posted by JosAH View Post
    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' .

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: While condition/math series.

    Quote Originally Posted by tnrh1 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: While condition/math series.

    the goal is to find the formula that will calculate the line number of 'n'
    It is Math.round(Math.sqrt(2*n))

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: While condition/math series.

    Quote Originally Posted by pbrockway2 View Post
    It is Math.round(Math.sqrt(2*n))
    Proof! We demand proof! And we don't buy "because it is so"! ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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

  20. #20
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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.)

Page 1 of 2 12 LastLast

Similar Threads

  1. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 07:23 AM
  2. Perrin series
    By moamen in forum New To Java
    Replies: 6
    Last Post: 12-04-2009, 06:27 PM
  3. Help with summing series
    By xplsivo in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 08:37 PM
  4. IF-Condition in a String
    By lenaz in forum Advanced Java
    Replies: 1
    Last Post: 07-18-2009, 01:07 PM
  5. How to add a second series in jfreechart
    By Manfizy in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 12:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •