Algorithm Problem Help

Status
Not open for further replies.

mikegg

Golden Member
Jan 30, 2010
1,886
501
136
Can someone write the solution in either Javascript, Python, or Ruby?

You have 100 dogs.

You have arranged all your dogs in a line. Initially, none of your dogs have any hats. You take 100 rounds walking around the dogs, always starting at the beginning. Every time you stop at a dog, you put a hat on it if it doesn't have one, and you take its hat off if it has one on.

The first round, you stop at every dog. The second round, you only stop at every 2nd dog (#2, #4, #6, #8, etc.). The third round, you only stop at every 3rd dog (#3, #6, #9, #12, etc.). You continue this process until the 100th round (i.e. you only visit the 100th dog).

Write a program that prints which dogs have hats at the end.

For example, if at the end dogs 5, 22, and 54 had hats, then the output would be:
5
22
54

I'm sure any number of people here can write the solution. But we don't like to do others' homework for them. If you want to take a shot at it, post your efforts, and ask questions that would be welcome.

Markbnj
Programming mod
 
Last edited by a moderator:
Status
Not open for further replies.