What I've been working on right now It was to enter a positive integer and output True if it was less than or equal to a multiple of 10 and False otherwise.

Below is my answer.

```
def near_ten(num)
basis = (num + 5) / 10 * 10
difference = (num - basis).abs
if difference <= 2
puts "True"
else
puts "False"
end
end
```

First, let `basis`

be the multiple of 10 that is closest to` num`

.

The second line calculates the numbers suitable for `basis`

.

```
basis = (num + 5) / 10 * 10
```

As an example, if it is an integer from 15 to 24, the nearest multiple of 10 is 20, so I want to use 20 as a reference.
Add 5 to each of these integers to get 20-29.
These integers have 2 in the tens place, so if you do `/ 10 * 10`

respectively, 20 will come out, which is the number you want to use as a reference.

Other integers work just as well.

This completes the `basis`

part.

Next, the difference between the original number and this standard is calculated.

```
num - basis
```

Since the difference may be negative as it is, we use a method that gives an absolute value.

```
(num - basis).abs
```

Then substitute this

```
difference = (num - basis).abs
```

If the difference `difference`

obtained in this way is 2 or less, it is sufficient, so it is judged and the process ends.

```
if difference <= 2
puts "True"
else
puts "False"
end
```

Recommended Posts