Biorhythms: Difference between revisions

10,500 bytes added ,  1 month ago
m
(New post.)
 
(9 intermediate revisions by 5 users not shown)
Line 423:
Age: 10717 days
Physical cycle: -27%
Emotional cycle: -100%</syntaxhighlight>
Intellectual cycle: -100%</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <chrono>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <numbers>
#include <sstream>
#include <string>
#include <vector>
 
const double tau = 2 * std::numbers::pi;
const std::vector<std::string> cycles { "PHYSICAL", "EMOTIONAL", "MENTAL" };
const std::vector<std::int32_t> cycle_lengths { 23, 28, 33 };
const std::vector<std::vector<std::string>> descriptions { { "up and rising", "peak" },
{ "up but falling", "transition" },
{ "down and falling", "valley" },
{ "down but rising", "transition" } };
 
std::string to_string(const std::chrono::sys_days& sys_date) {
std::stringstream stream;
stream << sys_date;
std::string iso_date;
stream >> iso_date;
return iso_date;
}
 
std::chrono::sys_days create_date(const std::string& iso_date) {
const int year = std::stoi(iso_date.substr(0, 4));
const unsigned int month = std::stoi(iso_date.substr(5, 7));
const unsigned int day = std::stoi(iso_date.substr(8, 10));
 
std::chrono::year_month_day date{std::chrono::year{year}, std::chrono::month{month}, std::chrono::day{day}};
return std::chrono::sys_days(date);
}
 
void biorhythms(const std::vector<std::string>& date_pair) {
std::chrono::sys_days birth_date = create_date(date_pair[0]);
std::chrono::sys_days target_date = create_date(date_pair[1]);
int32_t days_between = ( target_date - birth_date ).count();
std::cout << "Birth date " << birth_date << ", Target date " << target_date << std::endl;
std::cout << "Days between: " << days_between << std::endl;
 
for ( int32_t i = 0; i < 3; ++i ) {
const int32_t cycle_length = cycle_lengths[i];
const int32_t position_in_cycle = days_between % cycle_length;
const int32_t quadrant_index = 4 * position_in_cycle / cycle_length;
const int32_t percentage = round(100 * sin(tau * position_in_cycle / cycle_length));
 
std::string description;
if ( percentage > 95 ) {
description = "peak";
} else if ( percentage < -95 ) {
description = "valley";
} else if ( abs(percentage) < 5 ) {
description = "critical transition";
} else {
const int32_t days_to_transition = ( cycle_length * ( quadrant_index + 1 ) / 4 ) - position_in_cycle;
std::chrono::sys_days transition_date = target_date + std::chrono::days{days_to_transition};
std::string trend = descriptions[quadrant_index][0];
std::string next_transition = descriptions[quadrant_index][1];
description = std::to_string(percentage) + "% (" + trend + ", next " + next_transition
+ " " + to_string(transition_date) + ")";
}
 
std::cout << cycles[i] << " day " << position_in_cycle << ": " << description << std::endl;
}
std::cout << std::endl;
}
 
int main() {
const std::vector<std::vector<std::string>> date_pairs = {
{ "1943-03-09", "1972-07-11" },
{ "1809-01-12", "1863-11-19" },
{ "1809-02-12", "1863-11-19" } };
 
for ( const std::vector<std::string>& date_pair : date_pairs ) {
biorhythms(date_pair);
}
}
</syntaxhighlight>
{{ out }}
<pre>
Birth date 1943-03-09, Target date 1972-07-11
Days between: 10717
PHYSICAL day 22: -27% (down but rising, next transition 1972-07-12)
EMOTIONAL day 21: valley
MENTAL day 25: valley
 
Birth date 1809-01-12, Target date 1863-11-19
Days between: 20034
PHYSICAL day 1: 27% (up and rising, next peak 1863-11-23)
EMOTIONAL day 14: critical transition
MENTAL day 3: 54% (up and rising, next peak 1863-11-24)
 
Birth date 1809-02-12, Target date 1863-11-19
Days between: 20003
PHYSICAL day 16: -94% (down and falling, next valley 1863-11-20)
EMOTIONAL day 11: 62% (up but falling, next transition 1863-11-22)
MENTAL day 5: 81% (up and rising, next peak 1863-11-22)
</pre>
 
=={{header|COBOL}}==
{{trans|Locomotive Basic}}
Line 734 ⟶ 837:
readln;
end.</syntaxhighlight>
=={{header|EasyLang}}==
 
[https://easylang.online/show/#cod=bVNNj6MwDL3nVzwxaNUpgnX4aMuB4x72tn+hkIwaCUgFYabsr185QNtZ9ULs9+zYfia1GdwlRIVAlnkWUxZTGQh1dnoFj2lMx1jKQLxhg8epHt0AZzrN5ziPbEJCkniD+Jj6Buo8Q4VIBIAZFfqpq/WwpaoQEjlz3SvugJQ59YorF27Qbhp6ZIcj9pgRg8/djAi7DhHKdyjzCZkuZ44I6bHAHp33S0RQnJRRkZFIhNM3N5q/GvnSvumNQx3eR2hNr7+MchdQ4ut39lOjINDGsiOJ7hyheHCSNvfDDn6uOCU4i9SDW85tDQLQmKFpNSgpVuCGqEK6uMm9So7Sy8j9ow6f8NPpjiuPq/M8oloW4xcZe7v2f8BzIj0SOeUHAm8ET7KzLxJxHWyDZuZGe/vFFlyIxrZIlh4b29qBv0/3c9QeMkkRr+P7Wi78X+fiWS++n+NZNLajh3RXVEzsMZoeu+zApsJPLvS+hvglsLqIcH0paCJWfZbNL8/C6ySWAT2dZgj+XObRNOc2QEn0jTwh+NVZZ2zPLJXf2CxD8Lt3um114yYfQKUQ/wA= Run it]
 
<syntaxhighlight>
birth$ = "1943-03-09"
date$ = "1972-07-11"
# date$ = substr timestr systime 1 10
#
func day d$ .
y = number substr d$ 1 4
m = number substr d$ 6 2
d = number substr d$ 9 2
return 367 * y - 7 * (y + (m + 9) div 12) div 4 + 275 * m div 9 + d - 730530
.
textsize 4
func init b$ d$ .
linewidth 0.2
move 50 0
line 50 100
move 0 50
line 100 50
for d = -20 to 20
move x 50
circle 0.5
x += 2.5
.
move 4 94
text b$
move 4 88
text d$
days = day date$ - day birth$
move 4 80
text days & " days"
return days
.
proc cycle now cyc t$ col . .
color col
move 4 cyc * 1.2 - 20
text t$
linewidth 0.5
for d = now - 20 to now + 20
p = 20 * sin (360 * d / cyc)
line x 50 + p
x += 2.5
.
.
days = init birth$ date$
cycle days 23 "Physical" 900
cycle days 28 "Emotional" 090
cycle days 33 "Intellectual" 009
</syntaxhighlight>
 
=={{header|Emacs Lisp}}==
For Emacs, it makes sense to implement this as an interactive command which is personalized to the user's birthdate and computes the biorhythm for today. So if you put this code into your .emacs file (note that the birthdate has to be in MDY order in Emacs!):
Line 755 ⟶ 911:
age: 25238 physical: 94% emotional: 78% intellectual: -97%
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: calendar calendar.parser formatting io kernel math
Line 1,293 ⟶ 1,450:
 
public static void main(String[] aArgs) {
List<List<String>> datePairs = List.of(
List.of( "1943-03-09", "1972-07-11" ),
List.of( "1809-01-12", "1863-11-19" ),
Line 1,300 ⟶ 1,457:
for ( List<String> datePair : datePairs ) {
biorhythms(datePair);
System.out.println();
}
}
Line 1,316 ⟶ 1,472:
final int positionInCycle = daysBetween % cycleLength;
final int quadrantIndex = 4 * positionInCycle / cycleLength;
final int percentage = (int) Math.round(100 * Math.sin(2 * Math.PI * positionInCycle / cycleLength));
String description;
Line 1,334 ⟶ 1,490:
}
System.out.println(cycle + " day " + positionInCycle + ": " + description);
}
System.out.println();
}
Line 1,432 ⟶ 1,589:
Mental day 5: 81.5% (up and rising, next peak 1863-11-22)
</pre>
 
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="Kotlin">
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import kotlin.math.roundToInt
import kotlin.math.sin
 
fun main() {
val datePairs = listOf(
listOf("1943-03-09", "1972-07-11"),
listOf("1809-01-12", "1863-11-19"),
listOf("1809-02-12", "1863-11-19")
)
 
for (datePair in datePairs) {
calculateBiorhythms(datePair)
}
}
 
fun calculateBiorhythms(datePair: List<String>) {
val formatter = DateTimeFormatter.ISO_LOCAL_DATE
val birthDate = LocalDate.parse(datePair[0], formatter)
val targetDate = LocalDate.parse(datePair[1], formatter)
val daysBetween = ChronoUnit.DAYS.between(birthDate, targetDate).toInt()
println("Birth date $birthDate, Target date $targetDate")
println("Days between: $daysBetween")
 
for (cycle in Cycle.values()) {
val cycleLength = cycle.getLength()
val positionInCycle = daysBetween % cycleLength
val quadrantIndex = 4 * positionInCycle / cycleLength
val percentage = (100 * sin(2 * Math.PI * positionInCycle / cycleLength)).roundToInt()
 
val description = when {
percentage > 95 -> "peak"
percentage < -95 -> "valley"
Math.abs(percentage) < 5 -> "critical transition"
else -> {
val daysToTransition = (cycleLength * (quadrantIndex + 1) / 4) - positionInCycle
val transitionDate = targetDate.plusDays(daysToTransition.toLong())
val (trend, nextTransition) = cycle.descriptions(quadrantIndex)
"$percentage% ($trend, next $nextTransition $transitionDate)"
}
}
 
println("${cycle.name} day $positionInCycle: $description")
}
println()
}
 
enum class Cycle(private val length: Int) {
PHYSICAL(23), EMOTIONAL(28), MENTAL(33);
 
fun getLength() = length
 
fun descriptions(index: Int): Pair<String, String> {
val descriptions = listOf(
listOf("up and rising", "peak"),
listOf("up but falling", "transition"),
listOf("down and falling", "valley"),
listOf("down but rising", "transition")
)
return descriptions[index][0] to descriptions[index][1]
}
}
</syntaxhighlight>
{{out}}
<pre>
Birth date 1943-03-09, Target date 1972-07-11
Days between: 10717
PHYSICAL day 22: -27% (down but rising, next transition 1972-07-12)
EMOTIONAL day 21: valley
MENTAL day 25: valley
 
Birth date 1809-01-12, Target date 1863-11-19
Days between: 20034
PHYSICAL day 1: 27% (up and rising, next peak 1863-11-23)
EMOTIONAL day 14: critical transition
MENTAL day 3: 54% (up and rising, next peak 1863-11-24)
 
Birth date 1809-02-12, Target date 1863-11-19
Days between: 20003
PHYSICAL day 16: -94% (down and falling, next valley 1863-11-20)
EMOTIONAL day 11: 62% (up but falling, next transition 1863-11-22)
MENTAL day 5: 81% (up and rising, next peak 1863-11-22)
 
 
</pre>
 
 
=={{header|Locomotive Basic}}==
 
Line 2,110 ⟶ 2,361:
 
=={{header|REXX}}==
The &nbsp; '''daysbet2''' &nbsp; (REXX program) is used &nbsp; (invoked on line '''3''' of this program) &nbsp; to calculate the number of days between two dates,
<br>where the dates can be in several formats:
, (a comma) indicates today's date
* (an asterisk) indicates today's date
yyyy-mm-dd where <big><b>yyyy</b></big> may be a 2- or 4-digit year, <big><b>mm</b></big> may be a 1- or 2-digit month, <big><b>dd</b></big> may be a 1- or 2-digit day of month
mm/dd/yyyy (as above)
mm/dd (as above), but the current year is assumed
dd\mm\yyyy (as above)
dd\mm (as above), but the current year is assumed
 
It is usual to use the birth date of a person.
<syntaxhighlight lang="text">/*REXX pgm shows the states of a person's biorhythms (physical, emotional, intellectual) */
parse/* arg birthdate targetDate . /*obtain one or two dates(physical, fromemotional, theintellectual) C.L.*/
days=Parse Arg daysbet2(birthdate targetDate)targetdate . /* obtain one or two dates from CL /*invoke the 2nd version of a REXX pgm.*/
If birthdate='?' Then Do
if days==0 then do; say; say 'The two dates specified are exacty the same.'; exit 1
Say 'rexx bio birthdate (yyyymmdd) shows you today''s biorhythms'
end
Say 'or enter your birthday now'
cycles= 'physical emotional intellectual' /*the names of each biorhythm cycle*/
Parse Pull birthdate
cycle = 'negative neutral positive' /* " states of " " " */
If birthdate='' Then Exit
@.1= 23; @.2= 28; @.3= 33 /* " # of days in " " " */
End
pid2= pi() * 2 * days /*calculate pi * t * number─of─days. */
If birthdate='' Then
 
Parse Value 19460906 20200906 With birthdate targetdate
do j=1 for 3
If targetdate='' Then
state= 2 + sign( sin( pid2 / @.j) ) /*obtain state for each biorhythm cycle*/
targetdate=Date('S')
say 'biorhythm for the' right(word(cycles,j),12) "cycle is" word(cycle, state)
days=daysbet2(birthdate,targetdate)
end /*j*/ /* [↑] get state for each biorhythm. */
If days==0 Then Do
exit 0 /*stick a fork in it, we're all done. */
Say
/*──────────────────────────────────────────────────────────────────────────────────────*/
Say 'The two dates specified are exacty the same.'
pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923078; return pi
Exit 1
r2r: return arg(1) // (pi() * 2) /*normalize radians ──► a unit circle. */
End
/*──────────────────────────────────────────────────────────────────────────────────────*/
cycles='physical emotional intellectual' /*the biorhythm cycle names */
sin: procedure; parse arg x; x= r2r(x); _= x; numeric fuzz min(5, max(1, digits() -3))
cycle='negative neutral positive'
if x=pi * .5 then return 1; if x==pi*1.5 then return -1
period.1=23
if abs(x)=pi | x=0 then return 0; q= x*x; z= x
period.2=28
do k=2 by 2 until p=z; p= z; _= -_ *q/(k*k+k); z= z+_; end; return z</syntaxhighlight>
period.3=33
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 6/9/1946 </tt>}}
pid2=pi()*2*days
say 'Birthdate: ' birthdate '('translate('gh.ef.abcd',birthdate,'abcdefgh')')'
say 'Today: ' targetdate '('translate('gh.ef.abcd',targetdate,'abcdefgh')')'
Say 'Elapsed days:' days
Do j=1 To 3
state=2+sign(sin(pid2/period.j)) /* obtain state for each biorhythm */
Say 'biorhythm for the' right(word(cycles,j),12) 'cycle is',
word(cycle,state)
End
Exit
/*---------------------------------------------------------------------*/
pi:
pi=3.1415926535897932384626433832795028841971693993751058209749445923078
Return pi
r2r:
Return arg(1)//(pi()*2) /* normalize radians --? a unit ci*/
/*--------------------------------------------------------------------------------------*/
sin: Procedure
Parse Arg x
x=r2r(x)
_=x
Numeric Fuzz min(5,max(1,digits()-3))
If x=pi*.5 Then
Return 1
If x==pi*1.5 Then
Return-1
If abs(x)=pi|x=0 Then
Return 0
q=x*x
z=x
Do k=2 By 2 Until p=z
p=z
_=-_*q/(k*k+k)
z=z+_
End
Return z
 
daysbet2: Procedure
Note: &nbsp; today's date is being used, &nbsp; today is the 6<sup>th</sup> of September, 2020.
/* compute the number of days between fromdate and todate */
<pre>
Parse Arg fromdate,todate
fromday=date('B',fromdate,'S')
today=date('B',todate,'S')
Return today-fromday</syntaxhighlight>
{{out|output|text=&nbsp; when using the default dates}}
<pre>Birthdate: 19460906 (06.09.1946)
Today: 20200906 (06.09.2020)
Elapsed days: 27029
biorhythm for the physical cycle is positive
biorhythm for the emotional cycle is neutralpositive
biorhythm for the intellectual cycle is negativepositive</pre>
 
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'date'
Line 2,176 ⟶ 2,461:
mental : cycle day 25, negative
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import scala.collection.JavaConverters._
 
object Biorythms extends App {
 
val datePairs = List(
List("1943-03-09", "1972-07-11"),
List("1809-01-12", "1863-11-19"),
List("1809-02-12", "1863-11-19")
)
 
datePairs.foreach(biorhythms)
 
def biorhythms(aDatePair: List[String]): Unit = {
val formatter = DateTimeFormatter.ISO_LOCAL_DATE
val birthDate = LocalDate.parse(aDatePair.head, formatter)
val targetDate = LocalDate.parse(aDatePair(1), formatter)
val daysBetween = ChronoUnit.DAYS.between(birthDate, targetDate).toInt
println(s"Birth date $birthDate, Target date $targetDate")
println(s"Days between: $daysBetween")
 
for (cycle <- Cycle.values) {
val cycleLength = cycle.length
val positionInCycle = daysBetween % cycleLength
val quadrantIndex = 4 * positionInCycle / cycleLength
val percentage = Math.round(100 * Math.sin(2 * Math.PI * positionInCycle / cycleLength)).toInt
 
val description = if (percentage > 95) {
"peak"
} else if (percentage < -95) {
"valley"
} else if (Math.abs(percentage) < 5) {
"critical transition"
} else {
val daysToTransition = (cycleLength * (quadrantIndex + 1) / 4) - positionInCycle
val transitionDate = targetDate.plusDays(daysToTransition)
val descriptions = cycle.descriptions(quadrantIndex).asScala
val trend = descriptions.head
val nextTransition = descriptions(1)
s"$percentage% ($trend, next $nextTransition $transitionDate)"
}
 
println(s"${cycle} day $positionInCycle: $description")
}
println()
}
 
enum Cycle(val length: Int) {
case PHYSICAL extends Cycle(23)
case EMOTIONAL extends Cycle(28)
case MENTAL extends Cycle(33)
 
def descriptions(number: Int): java.util.List[String] = Cycle.DESCRIPTIONS.get(number)
}
 
object Cycle {
private val DESCRIPTIONS = java.util.List.of(
java.util.List.of("up and rising", "peak"),
java.util.List.of("up but falling", "transition"),
java.util.List.of("down and falling", "valley"),
java.util.List.of("down but rising", "transition")
)
}
 
}
</syntaxhighlight>
{{out}}
<pre>
Birth date 1943-03-09, Target date 1972-07-11
Days between: 10717
PHYSICAL day 22: -27% (down but rising, next transition 1972-07-12)
EMOTIONAL day 21: valley
MENTAL day 25: valley
 
Birth date 1809-01-12, Target date 1863-11-19
Days between: 20034
PHYSICAL day 1: 27% (up and rising, next peak 1863-11-23)
EMOTIONAL day 14: critical transition
MENTAL day 3: 54% (up and rising, next peak 1863-11-24)
 
Birth date 1809-02-12, Target date 1863-11-19
Days between: 20003
PHYSICAL day 16: -94% (down and falling, next valley 1863-11-20)
EMOTIONAL day 11: 62% (up but falling, next transition 1863-11-22)
MENTAL day 5: 81% (up and rising, next peak 1863-11-22)
 
 
</pre>
 
=={{header|Tcl}}==
{{works with|Wish}}
Line 2,428 ⟶ 2,808:
{{libheader|Wren-date}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./date" for Date
import "./fmt" for Fmt
 
var cycles = ["Physical day ", "Emotional day", "Mental day "]
1,983

edits