How to resolve the algorithm Biorhythms step by step in the Nim programming language
How to resolve the algorithm Biorhythms step by step in the Nim programming language
Table of Contents
Problem Statement
For a while in the late 70s, the pseudoscience of biorhythms was popular enough to rival astrology, with kiosks in malls that would give you your weekly printout. It was also a popular entry in "Things to Do with your Pocket Calculator" lists. You can read up on the history at Wikipedia, but the main takeaway is that unlike astrology, the math behind biorhythms is dead simple. It's based on the number of days since your birth. The premise is that three cycles of unspecified provenance govern certain aspects of everyone's lives – specifically, how they're feeling physically, emotionally, and mentally. The best part is that not only do these cycles somehow have the same respective lengths for all humans of any age, gender, weight, genetic background, etc, but those lengths are an exact number of days. And the pattern is in each case a perfect sine curve. Absolutely miraculous! To compute your biorhythmic profile for a given day, the first thing you need is the number of days between that day and your birth, so the answers in Days between dates are probably a good starting point. (Strictly speaking, the biorhythms start at 0 at the moment of your birth, so if you know time of day you can narrow things down further, but in general these operate at whole-day granularity.) Then take the residue of that day count modulo each of the the cycle lengths to calculate where the day falls on each of the three sinusoidal journeys. The three cycles and their lengths are as follows: The first half of each cycle is in "plus" territory, with a peak at the quarter-way point; the second half in "minus" territory, with a valley at the three-quarters mark. You can calculate a specific value between -1 and +1 for the kth day of an n-day cycle by computing sin( 2πk / n ). The days where a cycle crosses the axis in either direction are called "critical" days, although with a cycle value of 0 they're also said to be the most neutral, which seems contradictory. The task: write a subroutine, function, or program that will, given a birthdate and a target date, output the three biorhythmic values for the day. You may optionally include a text description of the position and the trend (e.g. "up and rising", "peak", "up but falling", "critical", "down and falling", "valley", "down but rising"), an indication of the date on which the next notable event (peak, valley, or crossing) falls, or even a graph of the cycles around the target date. Demonstrate the functionality for dates of your choice. Example run of my Raku implementation: Double valley! This was apparently not a good day for Mr. Fischer to begin a chess tournament...
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Biorhythms step by step in the Nim programming language
Source code in the nim programming language
import math
import strformat
import times
type Cycle {.pure.} = enum Physical, Emotional, Mental
const
Lengths: array[Cycle, int] = [23, 28, 33]
Quadrants = [("up and rising", "peak"),
("up but falling", "transition"),
("down and falling", "valley"),
("down but rising", "transition")]
DateFormat = "YYYY-MM-dd"
#---------------------------------------------------------------------------------------------------
proc biorythms(birthDate: DateTime; targetDate: DateTime = now()) =
## Display biorythms data. Arguments are DateTime values.
echo fmt"Born {birthDate.format(DateFormat)}, target date {targetDate.format(DateFormat)}"
let days = (targetDate - birthDate).inDays
echo "Day ", days
for cycle, length in Lengths:
let position = int(days mod length)
let quadrant = int(4 * position / length)
let percentage = round(100 * sin(2 * PI * (position / length)), 1)
var description: string
if percentage > 95:
description = "peak"
elif percentage < -95:
description = "valley"
elif abs(percentage) < 5:
description = "critical transition"
else:
let (trend, next) = Quadrants[quadrant]
let transition = targetDate + initDuration(days = (quadrant + 1) * length div 4 - position)
description = fmt"{percentage}% ({trend}, next {next} {transition.format(DateFormat)})"
echo fmt"{cycle} day {position}: {description}"
echo ""
#---------------------------------------------------------------------------------------------------
proc biorythms(birthDate, targetDate = "") =
## Display biorythms data. Arguments are strings in ISO format year-month-day.
let date = if targetDate.len == 0: now() else: targetDate.parse(DateFormat)
biorythms(birthDate.parse(DateFormat), date)
#———————————————————————————————————————————————————————————————————————————————————————————————————
when isMainModule:
biorythms("1943-03-09", "1972-07-11")
biorythms("1809-01-12", "1863-11-19")
biorythms("1809-02-12", "1863-11-19")
You may also check:How to resolve the algorithm Introspection step by step in the Inform 7 programming language
You may also check:How to resolve the algorithm Keyboard input/Flush the keyboard buffer step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the OCaml programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Clojure programming language
You may also check:How to resolve the algorithm Loops/For step by step in the REXX programming language