Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: Counting number of steps throughout the circle.

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Counting number of steps throughout the circle.

    The method public static int steps(int posts, int stride) calculates how many strides can be taken to get back to posts. Let's say if the method is (12, 4), it takes only three steps. Now let's say the method has parameters (12,5), so it should be (5, 10, 3, 8, 1, 6, 11, 4, 9, 2, 7, 12). My method works for such examplse as (12, 4) or (12,3) or (6,2)... but how can I figure out (12,5)?
    Java Code:
     public static int steps(int posts, int stride)
      {
    	  int countSteps = 0;
    	  int result = 0;
    	  do
    	  {
    		  result += stride; 
    
    		/*  if((result > posts) || (result < posts))
    		  {
    			  	
    		  }*/
    		  countSteps++;
    	  }
    	  while(result != posts);
    	  return countSteps;
      }
    Last edited by ssk2659; 01-27-2015 at 07:29 AM.

  2. #2
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Counting number of steps throughout the circle.

    What I think you should do is check when the number of strides goes past the number posts. For example, your example (12, 5) could go like this. once the number of strides reaches 12, whatever is left over would be the new stride variable(in this case 15-12=3). So you could have a temp variable that stores the number that goes pasts the number of posts.
    Last edited by Deathslice; 01-27-2015 at 07:20 AM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Counting number of steps throughout the circle.

    Well, I have no idea how your posts are laid out. Seems to me that if you divide the number of posts by the GCD of the two values you would get the number of strides. That would mean that (15,6) would require 5 strides (15 / 3 = 5). But I have no proof or foundation for this. But I'll bet that number theory provides a solution.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Counting number of steps throughout the circle.

    What I think you should do is check when the number of strides goes past the number posts. For example, your example (12, 5) could go like this. once the number of strides reaches 12, whatever is left over would be the new stride variable(in this case 15-12=3).
    That's what the % operator does. (10+5)%12 is 3. "result+=stride;" just needs a little tweak.

    Seems to me that if you divide the number of posts by the GCD of the two values you would get the number of strides. That would mean that (15,6) would require 5 strides (15 / 3 = 5). But I have no proof or foundation for this.
    (15,6) means "6 times what is a multiple of 15?". Dividing both numbers by their GCD gives a pair that are coprime (have no factors in common) (15,6) -> (5,2) Ie "2 times what is a multiple of 5?" Clearly the answers are the same with the second question talking about steps that are 3 times as big.

    The second form of the question - (5,2) - is easier to think about. Clearly the total distance walked is a multiple of both 5 and 2. But because they are coprime it must be a multiple of 10, of which the smallest is 10.

    So the answer to (5,2) and hence (15,6) is "5 strides, which will take you around the circle 2 times."

    ---

    The algebraic slogan is LCM(n,m) = (n*m) / GCD(n,m)

    So, n/GCD(n,m) = LCM(n,m)/m = distance/strideLength = number of strides as suggested

    And, m/GCD(n,m) = LCM(n,m)/n = distance/cirumference = number of times around the circle.
    Last edited by pbrockway2; 01-27-2015 at 09:10 AM.
    DarrylBurke and ssk2659 like this.

Similar Threads

  1. Replies: 1
    Last Post: 10-24-2012, 01:21 PM
  2. Counting number of user input
    By cs3 in forum New To Java
    Replies: 3
    Last Post: 10-24-2012, 10:33 AM
  3. Counting number of entries in a file
    By lenny0 in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 04:40 AM
  4. Replies: 4
    Last Post: 10-31-2010, 06:19 PM
  5. counting number of lines of system.out
    By IYIaster in forum New To Java
    Replies: 1
    Last Post: 07-21-2009, 01:37 AM

Posting Permissions

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