## Counting Fingers

### June 16, 2020

A little girl counts on her fingers in a curious way. She counts 1 on her thumb, 2 on her index finger, 3 on her middle finger, 4 on her ring finger, and 5 on her pinkie finger, then works back, counting 6 on her ring finger, 7 on her middle finger, 8 on her index finger, and 9 on her thumb, when she again turns around and counts 10 on her index finger, 11 on her middle finger, and so on.

Your task is to write a program that determines which finger the little girl will be on when she reaches a thousand. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

### 14 Responses to “Counting Fingers”

1. matthew said

I presume that should be: “counts 10 on her index finger, 11 on her middle finger, and so on”

2. programmingpraxis said

Correct. Fixed. Thank you.

3. Zack said

Here is my take on Julia: https://pastebin.com/NLWPwL5C

Cool drill for getting acquainted with modulo arithmetic. Cheers!

4. Mark said
```"timrprmip"[(999-1)%8]
```
5. Jan Van lent said
```f = lambda n: 4-abs(4-(n-1)%8)
print(f(1000))
print("timrp"[f(1000)]) # see Mark's solution
```
6. Xero said

My thorouhly documented solution implemented in SML can be found at <ahref=”https://pastebin.com/HQ5WkqKx”>. It takes a different approach from modular arithmetic.

7. Maksym Fedenko said

Hi, I’m beginner, can I resolve task in this way?

public class Exercises1 {
public static void main(String[] args) {
Solution solution = new Solution();
while (solution.getCounter_fingers() < 1006586890){
solution.setCounter_fingers(solution.getCounter_fingers() + 4);
solution.setCounter_iterations(solution.getCounter_iterations() + 1);
}
System.out.println(solution.getCounter_iterations());
solution.setCounter_fingers(solution.getCounter_fingers() – 1);
if(solution.getCounter_iterations() % 2 == 0){
System.out.println(“Girl stop count on index finger”);
}else {
System.out.println(“Girl stop count on ring finger”);
}
}

``````public static class Solution {
private int counter_fingers = 5;
private int counter_iterations = 1;
public int getCounter_fingers() {
return counter_fingers;
}
public void setCounter_fingers(int counter_fingers){
this.counter_fingers = counter_fingers;
}
public int getCounter_iterations() {
return counter_iterations;
}
public void setCounter_iterations(int counter_iterations) {
this.counter_iterations = counter_iterations;
}
}
``````
8. Larry Lee said

Using Spanish abbreviations for finger names (Haskell): `finger n = ['i', 'p', 'i', 'm', 'a', 'c', 'a', 'm'] !! (n`mod`8)`

9. Here’s Clojure solution with infinity lazy sequence of fingers :)

```(defn counting-fingers [n]
(let [fingers (cycle [:thumb :index :middle :ring
:pinky :ring :middle :index])]
(first (drop (dec n) fingers))))
```
10. ```(defn counting-fingers [n]
([:thumb :index :middle :ring
:pinky :ring :middle :index] (mod (dec n) 8)))
```
11. Daniel said

Here’s a solution in C.

```#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
assert(argc == 2);
int x = atoi(argv);
assert(x > 0);
char finger = "timrprmi"[(x - 1) % 8];
printf("%c\n", finger);
return EXIT_SUCCESS;
}
```

Usage:

```\$ ./a.out 1000
i
```
12. Antonio Leonti said

I took a very different approach from you all– my original thought was to just use x%8, but I decided to instead use the absolute value of a triangle wave. At least I thought it was neat.

```int finger(int x){
return (int)(fabs(8/M_PI * asin(sin(M_PI*(x-1) / 8)))+0.5);
}
int main(){
printf("%c\n", "TIMRP"[finger(1000)]);
}
```
13. Daniel said

Here’s a Python solution inspired by @Antonio’s solution, adapted to use integers in place of floating point.

```def finger(x):
return 'timrp'[abs(8 * ((x + 3) // 8) - x + 1)]

for x in range(1, 11):
print(x, finger(x))
print(1000, finger(1000))
```

Output:

```1 t
2 i
3 m
4 r
5 p
6 r
7 m
8 i
9 t
10 i
1000 i
```
14. Hakan said

In my approach, I took advantage of array pointers in PHP. Every time the pointer reaches an outer finger (thumb/pinky), the array elements are reversed in order. You simply scroll down to see the finger that is declared at count number 1000.

```<?php

// Put your fingers in the array
\$fingers = [
'thumb',
'index',
'middle',
'ring',
'pinky',
];

for (\$i = 1; \$i <= 1000; \$i++) {
// Declare both the count and finger with each iteration
echo \$i.". ".current(\$fingers)."<br>";
// Move the array pointer up one element
next(\$fingers);
// If the array pointer is at either the pinky or the thumb
if (current(\$fingers) == 'pinky' || current(\$fingers) == 'thumb') {
// reverse the order of the elements in the array
\$fingers = array_reverse(\$fingers);
}
}

?>

```