- #1

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