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

In summary: Read moreIn summary, the program is attempting to solve a problem where a worm crawls along an elastic rope that stretches by 100 meters each day. The worm crawls 6 meters each day, but the rope stretches, causing the worm to be farther from the starting point each day. The program uses a harmonic series to calculate the distance traveled by the worm each day, but there may be a problem with the inner while loop. The distance traveled should be 26 meters after 3 days, but the program is outputting 0 instead. Further examination and debugging is needed to determine the issue with the code.
  • #1
lycraa
17
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
  • #2
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:
  • #3
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
 
  • #4
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:
  • #5
this is exactly right, and a much better way of describing it.
 
  • #6
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.
 
  • #7
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
 
  • #8
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);
           }
}
 
  • #9
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.
 

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

1. What is the "Ant on a Rubber Band Program"?

The "Ant on a Rubber Band Program" is a simple simulation program that models the behavior of an ant crawling on a rubber band. It is often used as an introductory exercise in computer science courses to teach basic programming concepts.

2. How does the program work?

The program works by using a loop to continuously update the position of the ant as it crawls along the rubber band. The ant's position is determined by a variable called "distance" which increases or decreases depending on the direction the ant is crawling.

3. What is the purpose of the program?

The purpose of the program is to demonstrate how a simple set of instructions can create complex and unpredictable behavior. It also teaches students about basic programming concepts such as loops, variables, and conditional statements.

4. Can the program be modified or expanded upon?

Yes, the program can be modified or expanded upon in various ways. For example, you could add more ants, change the speed or direction of the ants, or even introduce obstacles for the ants to navigate around.

5. What skills can be gained from using the "Ant on a Rubber Band Program"?

Using the "Ant on a Rubber Band Program" can help develop skills such as problem-solving, critical thinking, and logical reasoning. It can also improve one's understanding of programming concepts and how to apply them to create simulations and models.

Similar threads

Replies
12
Views
2K
Replies
3
Views
2K
Replies
7
Views
2K
Replies
16
Views
13K
Replies
6
Views
1K
Back
Top