# Ant on a rubber band program

## 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 cant find it. This is a first year CS problem and im a 3rd year astrophysics major, this shouldn't be hard for me but ive had no luck. Please help :)

NascentOxygen
Staff Emeritus
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! 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

NascentOxygen
Staff Emeritus
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.

NascentOxygen
Staff Emeritus
there must be a problem with my code, but i cant 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

cepheid
Staff Emeritus
Gold Member
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:

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.

cepheid
Staff Emeritus
Gold Member
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.

woops, yes thats right, i ran it with a floating point number

is there a way do make a floating point number only display to a certain decimal place in java? im sure there is but i have no idea how.

cepheid
Staff Emeritus