Can a Worm Ever Reach the End of a Continuously Stretching Rope?

Click For Summary

Discussion Overview

The discussion revolves around a programming problem involving a worm crawling on a continuously stretching rope. Participants explore the mechanics of the scenario where the worm moves a fixed distance each day while the rope stretches, raising questions about the worm's ability to reach the end of the rope over time. The conversation includes attempts to write Java code to simulate the situation and analyze the underlying mathematical concepts.

Discussion Character

  • Homework-related
  • Mathematical reasoning
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the problem and attempts to derive a solution using a harmonic series, suggesting that the worm's progress can be modeled mathematically.
  • Another participant points out that while the worm crawls 6 meters each day, the rope stretches by 100 meters, complicating the worm's journey.
  • Some participants discuss the implications of the rope's stretching on the worm's position, with one explaining how the worm's distance from the start changes as the rope stretches overnight.
  • There are multiple attempts to write Java code to simulate the problem, with participants sharing their code and discussing issues related to integer arithmetic and variable types.
  • One participant identifies a problem with their code related to integer division, which leads to incorrect calculations of the distance traveled by the worm.
  • Another participant successfully resolves their coding issue by switching from an integer to a floating-point data type, leading to expected results.
  • Participants express uncertainty about formatting floating-point numbers in Java, indicating a lack of consensus on the best approach.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of the problem and the challenges posed by the stretching rope. However, there is no consensus on the best coding approach or the specific mathematical implications of the worm's journey, as multiple interpretations and coding solutions are presented.

Contextual Notes

Some participants mention the limitations of their code and the need for careful consideration of variable types in programming. There are unresolved questions regarding the mathematical modeling of the worm's progress and the implications of the stretching rope.

Who May Find This Useful

This discussion may be useful for students and programmers interested in mathematical modeling, coding challenges, and the application of algorithms in problem-solving contexts, particularly in Java.

lycraa
Messages
15
Reaction score
0

Homework Statement



Write a Java program to solve the following problem.
An elastic rope starts out with length 100 meters. The start of the rope is fixed to a pole and a worm is placed on the rope at the start and the worm starts crawling towards the end. Each day the worm crawls 6 meters along the rope. Each day the rope is stretched by 100 meters. After how many days does the worm reach the end of the rope?


Homework Equations



This is a harmonic series, so it will eventually have a solution. the equation i end up getting is:
for the nth term:
distance traveled = 6/(100+100) + 6/(100+200) + 6/(100+300)+...+6/(100+n100)


The Attempt at a Solution



attempt 1:
my code looks like:

ppublic class Worm2 {
public static void main(String[] arg) {

long day, distancefrend, length, n, j, i;

day = 1;
length = 100;
distancefrend = 0;
n = 1;
j = 0;
i = 0;

while (distancefrend >= 0){
while (n <= day){
j = 6/(100 + (n * 100));
i = i + j;
n++;
}
length = length + 100;
distancefrend = length - (i * length);
System.out.println("Day = " + day + " Distance from end: " + distancefrend + " meters.");
day++;
}
}
}


this code was going to take probably 3 hours to run, so i upped the time step to day = day + 1000000 but it STILL didnt work, my distance from end keeps growing forever ..so i tried a whole new approach:

attempt 2 (not using a harmonic series):

public class Worm {
public static void main(String[] arg) {

long day, distance, length, distancefrend;

day = 1;
length = 100;
distance = 0;
distancefrend = 0;

while (distance <= length){
length = length + (100);
distance = (distance/(length - 100))*length + (6);
distancefrend = length - distance;
System.out.println("Day = " + day + " Distance from end: " + distancefrend + "million meters.");
day++;
}
}
}

This was the same...there must be a problem with my code, but i can't find it. This is a first year CS problem and I am a 3rd year astrophysics major, this shouldn't be hard for me but I've had no luck. Please help :)
 
Physics news on Phys.org
Each day the worm progresses 6 metres, yet by day's end the rope has become 100 metres longer. On the face of it, it sounds like an exercise in futility! :smile: I'll have to ponder it further, since it has been set as an exercise.

That's one very elastic rope, by the way.

The theory seems to be that although the worm steps out 6 metres, at the end of the day the start of the rope has receded by more than 6 metres because the rope behind him has stretched, too.
 
Last edited:
put it this way:
the rope starts at 100 meters on day = 0, and the ant is at x = xo

over night the rope stretches to 200 meters,, so on day 2, the rope is 200m and in the morning the ant walks 6 meters along the rope so x = 6m.

over night the rope is stretched another 100m BUT the ant's distance has stretched also, which makes the rope at 300m, the ant at 9 meters. However the ant walks another 6 meters after the rope has been stretched. So, on day 3, the rope is 300m and the x = 15m...etc
 
Assuming I have the right idea, this is how I'd word it: each day the worm crawls 6 metres then builds a cocoon and sleeps away the cold night hours. Meanwhile, as he sleeps, the rope stretches an additional 100 metres overall. (This makes it clear how the cocoon gets carried along during the rope's stretching phase, moving him farther from the start even though he is comatose.)

EDITED (swapped day and night activities)
 
Last edited:
this is exactly right, and a much better way of describing it.
 
lycraa said:
there must be a problem with my code, but i can't find it.
The most obvious check would be to examine the first half dozen lines your program prints out. Compare with your hand calculations done on a calculator.
 
i think thers a problem with my inner while loop.

It i break it down to just:


public class Worm2 {
public static void main(String[] arg) {

long day, distance, length, n, j, i;

day = 1;
length = 100;
distancefrend = 0;
n = 1;
j = 0;
i = 0;

while (n <= 3){
j = 6/(100 + (n * 100));
i = i + j;
n++;
}

distance = i * 400;
system.out.print(distance);
}
}

it prints out that my distance traveled is 0, when it should be 26
 
lycraa said:
i think thers a problem with my inner while loop.

It i break it down to just:public class Worm2 {
public static void main(String[] arg) {

long day, distance, length, n, j, i;

day = 1;
length = 100;
distancefrend = 0;
n = 1;
j = 0;
i = 0;

while (n <= 3){
j = 6/(100 + (n * 100));
i = i + j;
n++;
}

distance = i * 400;
system.out.print(distance);
}
}

it prints out that my distance traveled is 0, when it should be 26

Isn't this just a problem with integer arithmetic? I mean, in your while loop, if n = 1, then j = 6/200 = 0 (in integer arithmetic). Also, could you please enclose your code in [noparse]
Code:
[/noparse] tags? It makes your code more readable, because it preserves your whitespace like this:

Code:
public class Worm2 {
	public static void main(String[] arg) {
  
		long day, distance, length, n, j, i;
  
		day = 1;
		length = 100;
		distancefrend = 0;
		n = 1;
		j = 0;
		i = 0;

                      while (n <= 3){
                            j = 6/(100 + (n * 100));
                            i = i + j;
                            n++;					
                      }

                      distance = i * 400;
                      system.out.print(distance);
           }
}
 
bingo! You got it :) I ran it with int instead of int and it worked beautifully. I ended up with 26.4 million days, which is what i was expecting. its my first time dealing with the long variable, I'm not sure why i didn't pick up on that.

Sorry about the code, its my first time posting code in a forum, i had no idea.
 
  • #10
lycraa said:
bingo! You got it :) I ran it with int instead of int and it worked beautifully. I ended up with 26.4 million days, which is what i was expecting. its my first time dealing with the long variable, I'm not sure why i didn't pick up on that.

Sorry about the code, its my first time posting code in a forum, i had no idea.

No problem. Did you mean that you ran it with float instead of int? It wouldn't work with any integer data type (short, int, or long, or any of their unsigned variants).

EDIT: Disclaimer: My specific data type examples are for C, which is the only language I really know well. But the statement that you'd need some sort of floating point data type is generally true.
 
  • #11
woops, yes that's right, i ran it with a floating point number
 
  • #12
is there a way do make a floating point number only display to a certain decimal place in java? I am sure there is but i have no idea how.
 
  • #13
lycraa said:
is there a way do make a floating point number only display to a certain decimal place in java? I am sure there is but i have no idea how.

If have no idea how either. There almost certainly is though (I mean, I know you can do it in C, so you ought to be able to do it in Java). You'll have to look up some info on the print function.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
13K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 53 ·
2
Replies
53
Views
6K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
29
Views
10K