Stuck on a MIPS recursive function - recursion

I'm doing an assignment in MIPS and I feel like my logic is sound but I just can't get the code to produce the correct output. Can anyone look over what I've got and help? This is the corresponding function in C that I'm trying to implement.
int function1(int n) {
if (n <= 3) {
int ans1 = (3*n)-5;
return ans1;
}
else {
int ans1 = (n-1)*function1(n-1) + function1(n-2) - n;
return ans1;
}
}
This is what I've got in MIPS. For the sample output, when the user enters 8 the output is supposed to be 1096. I get 22,735 so obviously I'm messing up big time.
.data
message1: .asciiz "Enter an integer\n"
message2: .asciiz "The integer is: "
.text
.globl main
main:
la $a0, message1
li $v0, 4
syscall # print message1 string
li $v0, 5
syscall # get user input
move $a0, $v0 # int n
jal function # call function
move $s0, $v0 # save function return value
la $a0, message2
li $v0, 4
syscall # print message2 string
move $a0, $s0
li $v0, 1
syscall # print the solution computed by the recursive function
li $v0, 10
syscall # exit
function:
addi $sp, $sp, -12 # allocate memory on stack
sw $ra, 0($sp) # save return address
sw $a0, 4($sp) # save argument
ble $a0, 3, base_case # if n is <= 3 go to base case
addi $a0, $a0, -1 # n -= 1
jal function # recursive call
lw $a0, 4($sp) # load n
mul $v0, $v0, $a0 # (n-1) * function(n-1)
sw $v0, 8($sp) # save result
lw $a0, 4($sp) # load n
addi $a0, $a0, -2 # set n -= 2
jal function # recursive call
lw $t0, 8($sp) # load (n-1) * function(n-1)
add $v0, $v0, $t0 # add f(n-2) and (n-1) * f(n-1)
lw $t1, 4($sp) # load n
sub $v0, $v0, $t1 # (n-1) * f(n-1) + f(n-2) - n
lw $ra, 0($sp) # load return address
addi $sp, $sp, 12 # free stack
jr $ra # return
base_case:
li $t5, 3
mul $v0, $a0, $t5 # (n*3)
addi $v0, $v0, -5 # (n*3) - 5
lw $ra, 0($sp) # load return address
addi $sp, $sp, 12 # free stack
jr $ra # return

when the user enters 8 the output is supposed to be 1096.
Before considering your MIPS code, your C code doesn't appear to produce a correct result either:
#include <stdio.h>
int function1(int n) {
if (n <= 3) {
return 3 * n - 5;
}
return (n - 1) * function1(n - 1) + function1(n - 2) - n;
}
int main() {
printf("%d\n", function1(8));
return 0;
}
This code prints 7842, not the 1096 you claim. Perhaps this is where we should begin debugging.
Double checking this by hand:
function1(8)
7 * function1(7) + function1(6) - 8
7 * (6 * function1(6) + function1(5) - 7) + (5 * function1(5) + function1(4) - 6) - 8
7 * (6 * (5 * function1(5) + function1(4) - 6) + (4 * function1(4) + function1(3) - 5) - 7) + (5 * (4 * function1(4) + function1(3) - 5) + (3 * function1(3) + function1(2) - 4) - 6) - 8
7 * (6 * (5 * (4 * function1(4) + function1(3) - 5) + (3 * function1(3) + function1(2) - 4) - 6) + (4 * (3 * function1(3) + function1(2) - 4) + 4 - 5) - 7) + (5 * (4 * (3 * function1(3) + function1(2) - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) - 8
7 * (6 * (5 * (4 * (3 * function1(3) + function1(2) - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) + (4 * (3 * 4 + 1 - 4) + 4 - 5) - 7) + (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) - 8
7 * (6 * (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) + (4 * (3 * 4 + 1 - 4) + 4 - 5) - 7) + (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) - 8
> python3
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 7 * (6 * (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) + (4 * (3 * 4 + 1 - 4) + 4 - 5) - 7) + (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) - 8
7842
>>>

Related

MIPS Assembly - how to calculate the square of n recursively

I want to write a program that calculates the square of n using recursion based on the equation n^2 = (n - 1)^2 + 2(n - 1) + 1 But I don't know how to write the nonbasecase: part. Can anyone help?
A python program would be
def square(n) {
if (n==0):
return 0
else:
return square(n-1) + 2*(n-1) + 1
}
Here is what I got so far.
start:
li $a0, 0x0003 #$a0 contains the number to be squared
jal square # recursive call
square:
subi $sp, $sp, 8 # decrement the stack pointer $sp
sw $ra, 4($sp) # push the return address register $ra
sw $a0, 0($sp) # push argument register $a0
li $t0, 0x0001 # load $t0 with 1 as part of test for base case
bne $a0, $t0, nonbasecase # branch if not the base case
li $v0, 0x0001 # return base result in $v0
addi $sp, $sp 8 # recover stack space
jr $ra # jump to return address in $ra
nonbasecase:
#not sure how to write when it is not the base case
jr $ra # jump to contents of return address register $ra
Write the function so that it inputs n2 and n and returns n2+2*n+1 and n+1.

Recursive program in RISC-V (procedure call)

I'm trying to implement the recursive equation here, but I have no idea what's going wrong with my code.
It keeps output 1 no matter what input I gave.
The function is as follows:
T(n) = T(n-100) + 2*T(n/2) + 5 if n > 1
= 1 otherwise
.globl __start
.text
__start:
# read from standard input a0 = n
addi a0, zero, 5
ecall
################################################################################
# Recursive function #
# T(n) = T(n-100)+2*T((n/2))+5 if n>1 #
# = 1 otherwise #
################################################################################
addi s2,x0,1 #for base case
addi s3,x0,2 #for comparison
blt a0,s3,result
recur:
addi a2,a2,5
addi a3,a3,9
addi sp,sp,-8 #store 2 registers into stack
sw x1,4(sp)
sw a0,0(sp)
bge a0,s3,L1
bge a0,s3,L2
addi s2,x0,1
addi sp,sp,8
jal x0,result
L1: #to do T(n-100)
addi a0,a0,-100
jal x1,recur
addi x6,x10,0 #T(n-100) stored in x6
lw x10,0(sp)
lw x1,4(sp)
addi sp,sp,8
jalr x0,0(x1)
L2: #to do T(n/2)
div a0,a0,s3
jal x1,recur
addi x7,x10,0 #T(n/2) stored in x7
lw a0,0(sp)
lw x1,4(sp)
addi sp,sp,8
mul x7,x7,s3
add s2,x6,x7
addi s2,s2,5
jalr x0,0(x1)
################################################################################
result:
# prints the result in s2
addi a0, zero, 1
addi a1, s2, 0
ecall
# ends the program with status code 0
addi a0, zero, 10
ecall

Qbasic GOTO to a line with Apostophe

I'm trying to "convert" an old program in QBasic to Java.
I have a problem while debugging and I think is related to this GOTO statement.
I know that a line with an apostrophe is ignored but in this case the statement GOTO targets that line. Is ignored the first time and after with the GOTO statement is not?And the apostrophe concerns only the variable R12 and X12 or also R22,X22?
This is the .BAS file :)
*
4010 'R12 = R2MA + ((R2A * R2M * (R2A + R2M) + S ^ 2 * (R2A * X2M ^ 2 + R2M * X2A ^ 2)) / ((R2A + R2M) ^ 2 + S ^ 2 * (X2A + X2M) ^ 2))
4014 'X12 = X2D + ((R2A ^ 2 * X2M + R2M ^ 2 * X2A + S ^ 2 * X2A * X2M * (X2A + X2M)) / ((R2A + R2M) ^ 2 + S ^ 2 * (X2A + X2M) ^ 2))
R22 = ((R2A * XMMU ^ 2 + R2M * XAVVMU ^ 2) * S ^ 2 + R2A * R2M * RT2) / (RT2 ^ 2 + S ^ 2 * XT2 ^ 2)
X22 = (X2A * (R2M ^ 2 + S ^ 2 * X2M ^ 2) + X2M * (R2A ^ 2 + S ^ 2 * X2A ^ 2) + 2 * X2AVM * (R2A * R2M - S ^ 2 * X2AVM * XT2)) / (RT2 ^ 2 + S ^ 2 * XT2 ^ 2)
X22 = X22 + X2D
R22 = R22 / ZUN: X22 = X22 / ZUN
4016 A1 = R22 * E1 ^ 2 / (P2 * X22 ^ 2 + R22 * E1 ^ 2)
4020 S = .5 * A1 - SQR(A1 * (.25 * A1 - R22 * P2 / E1 ^ 2))
4030 G1 = (PFE / (3 * (VF * (FR / 100)) ^ 2)) * ZUN
4040 A2 = (R22 / S) ^ 2 + X22 ^ 2: B2 = R22 / (S * A2): C2 = X22 / A2: D2 = B2 + G1
4050 E2 = C2 + B1: F2 = D2 ^ 2 + E2 ^ 2: G2 = D2 / F2: H2 = E2 / F2: Z2 = SQR(G2 ^ 2 + H2 ^ 2)
4060 I2 = G2 + R11: L = H2 + X11: ZTOT = SQR(I2 ^ 2 + L ^ 2)
4070 I1 = 1 / ZTOT
4080 E3 = I1 * Z2
4090 IF ABS(E1 - E3) > P0 THEN 5800
.
.
.
5800 E1 = (E1 + E3) / 2
5810 GOTO 4010
*
This sure is an odd way to write a QBasic program, usually you either number all of the lines or none of them. You can run a small test program to see how QB handles GOTO in this case. So something like this:
10 I = 0
20 'PRINT "Test line 20"
30 'PRINT "Test line 30
PRINT "Test line 40"
PRINT "Test line 50"
60 PRINT "Test line 60"
70 I = I + 1
80 IF (I <= 1) GOTO 20
90 PRINT "THE END"
I tried this in QB4.5 and the output is this:
Test line 40
Test line 50
Test line 60
Test line 40
Test line 50
Test line 60
THE END
The GOTO statement works as normal but the contents of lines 20 and 30 are not executed, it is treated as comment. So to answer your question, in your case the lines with R12 = .. and X12 = .. will be ignored and not executed, and the lines with R22 = .. and X22 = .. etc. are processed as normal.

MIPS Recursive Fibonacci Sequence

I'm having trouble dealing with stacks recursively in MIPS. I get the concept, but my program isn't reacting as I mean it to.
My goal is to take user input as n and print the Fibonacci number at n. What I have so far is below.
(I'm fairly certain the problem is in the actual calculation of the number in the fib function.) Thanks for any help! :)
.text
main:
# Prompt user to input non-negative number
la $a0,prompt
li $v0,4
syscall
li $v0,5
syscall
move $t2,$v0
# Call function to get fibonnacci #n
move $a0,$t2
move $v0,$t2
jal fib
move $t3,$v0
# Output message and n
la $a0,result
li $v0,4
syscall
move $a0,$t2
li $v0,1
syscall
la $a0,result2
li $v0,4
syscall
move $a0,$t3
li $v0,1
syscall
la $a0,endl
li $v0,4
syscall
# End program
li $v0,10
syscall
fib:
# Compute and return fibonacci number
beqz $a0,zero
beq $a0,1,one
sub $sp,$sp,4
sw $ra,0($sp)
sub $a0,$a0,1
jal fib
lw $ra,0($sp)
add $sp,$sp,4
sub $t8,$v0,2 # n - 2
sub $t9,$v0,1 # n - 1
add $v0,$t8,$t9 # add n-2,n-1
jr $ra # decrement/next in stack
zero:
li $v0,0
jr $ra
one:
li $v0,1
jr $ra
.data
prompt: .asciiz "Enter a non-negative number: "
result: .asciiz "F_"
result2: .asciiz " = "
endl: .asciiz "\n"
Example runs:
Enter a non-negative number: 5
F_5 = -29
Enter a non-negative number: 6
F_6 = -61
Correct runs:
Enter a non-negative number: 5
F_5 = 5
Enter a non-negative number: 6
F_6 = 8
Here is a properly working code:
.text
main:
# Prompt user to input non-negative number
la $a0,prompt
li $v0,4
syscall
li $v0,5 #Read the number(n)
syscall
move $t2,$v0 # n to $t2
# Call function to get fibonnacci #n
move $a0,$t2
move $v0,$t2
jal fib #call fib (n)
move $t3,$v0 #result is in $t3
# Output message and n
la $a0,result #Print F_
li $v0,4
syscall
move $a0,$t2 #Print n
li $v0,1
syscall
la $a0,result2 #Print =
li $v0,4
syscall
move $a0,$t3 #Print the answer
li $v0,1
syscall
la $a0,endl #Print '\n'
li $v0,4
syscall
# End program
li $v0,10
syscall
fib:
# Compute and return fibonacci number
beqz $a0,zero #if n=0 return 0
beq $a0,1,one #if n=1 return 1
#Calling fib(n-1)
sub $sp,$sp,4 #storing return address on stack
sw $ra,0($sp)
sub $a0,$a0,1 #n-1
jal fib #fib(n-1)
add $a0,$a0,1
lw $ra,0($sp) #restoring return address from stack
add $sp,$sp,4
sub $sp,$sp,4 #Push return value to stack
sw $v0,0($sp)
#Calling fib(n-2)
sub $sp,$sp,4 #storing return address on stack
sw $ra,0($sp)
sub $a0,$a0,2 #n-2
jal fib #fib(n-2)
add $a0,$a0,2
lw $ra,0($sp) #restoring return address from stack
add $sp,$sp,4
#---------------
lw $s7,0($sp) #Pop return value from stack
add $sp,$sp,4
add $v0,$v0,$s7 # f(n - 2)+fib(n-1)
jr $ra # decrement/next in stack
zero:
li $v0,0
jr $ra
one:
li $v0,1
jr $ra
.data
prompt: .asciiz "This program calculates Fibonacci sequence with recursive functions.\nEnter a non-negative number: "
result: .asciiz "F_"
result2: .asciiz " = "
endl: .asciiz "\n"
Hope to be usefull
Adel Zare
adel.zare.63 [at] gmail [dot] com
You appear to have misunderstood the algorithm (or just implemented it incorrectly). What you're doing is this:
int fib(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
int ret = fib(n - 1);
return (ret - 2) + (ret - 1);
}
What you should be doing is this:
int fib(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
return fib(n - 1) + fib(n - 2);
}

Function to return date of Easter for the given year

So, here's a funny little programming challenge. I was writing a quick method to determine all the market holidays for a particular year, and then I started reading about Easter and discovered just how crazy* the logic is for determining its date--the first Sunday after the Paschal Full Moon following the spring equinox! Does anybody know of an existing function to calculate the date of Easter for a given year?
Granted, it's probably not all that hard to do; I just figured I'd ask in case somebody's already done this. (And that seems very likely.)
UPDATE: Actually, I'm really looking for the date of Good Friday (the Friday before Easter)... I just figured Easter would get me there. And since I'm in the U.S., I assume I'm looking for the Catholic Easter? But perhaps someone can correct me on that if I'm wrong.
*By "crazy" I meant, like, involved. Not anything offensive...
Python: using dateutil's easter() function.
>>> from dateutil.easter import *
>>> print easter(2010)
2010-04-04
>>> print easter(2011)
2011-04-24
The functions gets, as an argument, the type of calculation you like:
EASTER_JULIAN = 1
EASTER_ORTHODOX = 2
EASTER_WESTERN = 3
You can pick the one relevant to the US.
Reducing two days from the result would give you Good Friday:
>>> from datetime import timedelta
>>> d = timedelta(days=-2)
>>> easter(2011)
datetime.date(2011, 4, 24)
>>> easter(2011)+d
datetime.date(2011, 4, 22)
Oddly enough, someone was iterating this, and published the results in Wikipedia's article about the algorithm:
in SQL Server Easter Sunday would look like this, scroll down for Good Friday
CREATE FUNCTION dbo.GetEasterSunday
( #Y INT )
RETURNS SMALLDATETIME
AS
BEGIN
DECLARE #EpactCalc INT,
#PaschalDaysCalc INT,
#NumOfDaysToSunday INT,
#EasterMonth INT,
#EasterDay INT
SET #EpactCalc = (24 + 19 * (#Y % 19)) % 30
SET #PaschalDaysCalc = #EpactCalc - (#EpactCalc / 28)
SET #NumOfDaysToSunday = #PaschalDaysCalc - (
(#Y + #Y / 4 + #PaschalDaysCalc - 13) % 7
)
SET #EasterMonth = 3 + (#NumOfDaysToSunday + 40) / 44
SET #EasterDay = #NumOfDaysToSunday + 28 - (
31 * (#EasterMonth / 4)
)
RETURN
(
SELECT CONVERT
( SMALLDATETIME,
RTRIM(#Y)
+ RIGHT('0'+RTRIM(#EasterMonth), 2)
+ RIGHT('0'+RTRIM(#EasterDay), 2)
)
)
END
GO
Good Friday is like this and it uses the Easter function above
CREATE FUNCTION dbo.GetGoodFriday
(
#Y INT
)
RETURNS SMALLDATETIME
AS
BEGIN
RETURN (SELECT dbo.GetEasterSunday(#Y) - 2)
END
GO
From here: http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html
When it came for me to write this (traffic prediction based on day of week and holiday),
I gave up on trying to write it by myself. I found it somewhere on the net. The code was public domain, but...
sigh
see for yourself.
void dateOfEaster(struct tm* p)
{
int Y = p->tm_year;
int a = Y % 19;
int b = Y / 100;
int c = Y % 100;
int d = b / 4;
int e = b % 4;
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int L = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * L) / 451;
p->tm_mon = ((h + L - 7 * m + 114) / 31 ) - 1;
p->tm_mday = ((h + L - 7 * m + 114) % 31) + 1;
p->tm_hour = 12;
const time_t tmp = mktime(p);
*p = *localtime(&tmp); //recover yday from mon+mday
}
Some questions are better left unasked.
I feel lucky that all moving holidays in my country are a fixed offset from the date of Easter.
The SQL Server function below is more general than the accepted answer
The accepted answer is only correct for the range (inclusive) : 1900-04-15 to 2099-04-12
It uses the algorithm provided by The United States Naval Observatory (USNO)
http://aa.usno.navy.mil/faq/docs/easter.php
CREATE FUNCTION dbo.GetEasterSunday (#Y INT)
RETURNS DATETIME
AS
BEGIN
-- Source of algorithm : http://aa.usno.navy.mil/faq/docs/easter.php
DECLARE #c INT = #Y / 100
DECLARE #n INT = #Y - 19 * (#Y / 19)
DECLARE #k INT = (#c - 17) / 25
DECLARE #i INT = #c - #c / 4 - (#c - #k) / 3 + 19 * #n + 15
SET #i = #i - 30 * (#i / 30)
SET #i = #i - (#i / 28) * (1 - (#i / 28) * (29 / (#i + 1)) * ((21 - #n) / 11))
DECLARE #j INT = #Y + #Y / 4 + #i + 2 - #c + #c / 4
SET #j = #j - 7 * (#j / 7)
DECLARE #l INT = #i - #j
DECLARE #m INT = 3 + (#l + 40) / 44
DECLARE #d INT = #l + 28 - 31 * (#m / 4)
RETURN
(
SELECT CONVERT
( DATETIME,
RTRIM(#Y)
+ RIGHT('0'+RTRIM(#m), 2)
+ RIGHT('0'+RTRIM(#d), 2)
)
)
END
GO
VB .NET Functions for Greek Orthodox and Catholic Easter:
Public Shared Function OrthodoxEaster(ByVal Year As Integer) As Date
Dim a = Year Mod 19
Dim b = Year Mod 7
Dim c = Year Mod 4
Dim d = (19 * a + 16) Mod 30
Dim e = (2 * c + 4 * b + 6 * d) Mod 7
Dim f = (19 * a + 16) Mod 30
Dim key = f + e + 3
Dim month = If((key > 30), 5, 4)
Dim day = If((key > 30), key - 30, key)
Return New DateTime(Year, month, day)
End Function
Public Shared Function CatholicEaster(ByVal Year As Integer) As DateTime
Dim month = 3
Dim a = Year Mod 19 + 1
Dim b = Year / 100 + 1
Dim c = (3 * b) / 4 - 12
Dim d = (8 * b + 5) / 25 - 5
Dim e = (5 * Year) / 4 - c - 10
Dim f = (11 * a + 20 + d - c) Mod 30
If f = 24 Then f += 1
If (f = 25) AndAlso (a > 11) Then f += 1
Dim g = 44 - f
If g < 21 Then g = g + 30
Dim day = (g + 7) - ((e + g) Mod 7)
If day > 31 Then
day = day - 31
month = 4
End If
Return New DateTime(Year, month, day)
End Function
The below code determines Easter through powershell:
function Get-DateOfEaster {
param(
[Parameter(ValueFromPipeline)]
$theYear=(Get-Date).Year
)
if($theYear -lt 1583) {
return $null
} else {
# Step 1: Divide the theYear by 19 and store the
# remainder in variable A. Example: If the theYear
# is 2000, then A is initialized to 5.
$a = $theYear % 19
# Step 2: Divide the theYear by 100. Store the integer
# result in B and the remainder in C.
$c = $theYear % 100
$b = ($theYear -$c) / 100
# Step 3: Divide B (calculated above). Store the
# integer result in D and the remainder in E.
$e = $b % 4
$d = ($b - $e) / 4
# Step 4: Divide (b+8)/25 and store the integer
# portion of the result in F.
$f = [math]::floor(($b + 8) / 25)
# Step 5: Divide (b-f+1)/3 and store the integer
# portion of the result in G.
$g = [math]::floor(($b - $f + 1) / 3)
# Step 6: Divide (19a+b-d-g+15)/30 and store the
# remainder of the result in H.
$h = (19 * $a + $b - $d - $g + 15) % 30
# Step 7: Divide C by 4. Store the integer result
# in I and the remainder in K.
$k = $c % 4
$i = ($c - $k) / 4
# Step 8: Divide (32+2e+2i-h-k) by 7. Store the
# remainder of the result in L.
$l = (32 + 2 * $e + 2 * $i - $h - $k) % 7
# Step 9: Divide (a + 11h + 22l) by 451 and
# store the integer portion of the result in M.
$m = [math]::floor(($a + 11 * $h + 22 * $l) / 451)
# Step 10: Divide (h + l - 7m + 114) by 31. Store
# the integer portion of the result in N and the
# remainder in P.
$p = ($h + $l - 7 * $m + 114) % 31
$n = (($h + $l - 7 * $m + 114) - $p) / 31
# At this point p+1 is the day on which Easter falls.
# n is 3 for March and 4 for April.
$DateTime = New-Object DateTime $theyear, $n, ($p+1), 0, 0, 0, ([DateTimeKind]::Utc)
return $DateTime
}
}
$eastersunday=Get-DateOfEaster 2015
Write-Host $eastersunday
Found this Excel formula somewhere
Assuming cell A1 contains year e.g. 2020
ROUND(DATE(A1;4;1)/7+MOD(19*MOD(A1;19)-7;30)*0,14;0)*7-6
Converted to T-SQL lead me to this:
DECLARE #yr INT=2020
SELECT DATEADD(dd, ROUND(DATEDIFF(dd, '1899-12-30', DATEFROMPARTS(#yr, 4, 1)) / 7.0 + ((19.0 * (#yr % 19) - 7) % 30) * 0.14, 0) * 7.0 - 6, -2)
In JS, taken from here.
var epoch=2444238.5,elonge=278.83354,elongp=282.596403,eccent=.016718,sunsmax=149598500,sunangsiz=.533128,mmlong=64.975464,mmlongp=349.383063,mlnode=151.950429,minc=5.145396,mecc=.0549,mangsiz=.5181,msmax=384401,mparallax=.9507,synmonth=29.53058868,lunatbase=2423436,earthrad=6378.16,PI=3.141592653589793,epsilon=1e-6;function sgn(x){return x<0?-1:x>0?1:0}function abs(x){return x<0?-x:x}function fixAngle(a){return a-360*Math.floor(a/360)}function toRad(d){return d*(PI/180)}function toDeg(d){return d*(180/PI)}function dsin(x){return Math.sin(toRad(x))}function dcos(x){return Math.cos(toRad(x))}function toJulianTime(date){var year,month,day;year=date.getFullYear();var m=(month=date.getMonth()+1)>2?month:month+12,y=month>2?year:year-1,d=(day=date.getDate())+date.getHours()/24+date.getMinutes()/1440+(date.getSeconds()+date.getMilliseconds()/1e3)/86400,b=isJulianDate(year,month,day)?0:2-y/100+y/100/4;return Math.floor(365.25*(y+4716)+Math.floor(30.6001*(m+1))+d+b-1524.5)}function isJulianDate(year,month,day){if(year<1582)return!0;if(year>1582)return!1;if(month<10)return!0;if(month>10)return!1;if(day<5)return!0;if(day>14)return!1;throw"Any date in the range 10/5/1582 to 10/14/1582 is invalid!"}function jyear(td,yy,mm,dd){var z,f,alpha,b,c,d,e;return f=(td+=.5)-(z=Math.floor(td)),b=(z<2299161?z:z+1+(alpha=Math.floor((z-1867216.25)/36524.25))-Math.floor(alpha/4))+1524,c=Math.floor((b-122.1)/365.25),d=Math.floor(365.25*c),e=Math.floor((b-d)/30.6001),{day:Math.floor(b-d-Math.floor(30.6001*e)+f),month:Math.floor(e<14?e-1:e-13),year:Math.floor(mm>2?c-4716:c-4715)}}function jhms(j){var ij;return j+=.5,ij=Math.floor(86400*(j-Math.floor(j))+.5),{hour:Math.floor(ij/3600),minute:Math.floor(ij/60%60),second:Math.floor(ij%60)}}function jwday(j){return Math.floor(j+1.5)%7}function meanphase(sdate,k){var t,t2;return 2415020.75933+synmonth*k+1178e-7*(t2=(t=(sdate-2415020)/36525)*t)-155e-9*(t2*t)+33e-5*dsin(166.56+132.87*t-.009173*t2)}function truephase(k,phase){var t,t2,t3,pt,m,mprime,f,apcor=!1;if(pt=2415020.75933+synmonth*(k+=phase)+1178e-7*(t2=(t=k/1236.85)*t)-155e-9*(t3=t2*t)+33e-5*dsin(166.56+132.87*t-.009173*t2),m=359.2242+29.10535608*k-333e-7*t2-347e-8*t3,mprime=306.0253+385.81691806*k+.0107306*t2+1236e-8*t3,f=21.2964+390.67050646*k-.0016528*t2-239e-8*t3,phase<.01||abs(phase-.5)<.01?(pt+=(.1734-393e-6*t)*dsin(m)+.0021*dsin(2*m)-.4068*dsin(mprime)+.0161*dsin(2*mprime)-4e-4*dsin(3*mprime)+.0104*dsin(2*f)-.0051*dsin(m+mprime)-.0074*dsin(m-mprime)+4e-4*dsin(2*f+m)-4e-4*dsin(2*f-m)-6e-4*dsin(2*f+mprime)+.001*dsin(2*f-mprime)+5e-4*dsin(m+2*mprime),apcor=!0):(abs(phase-.25)<.01||abs(phase-.75)<.01)&&(pt+=(.1721-4e-4*t)*dsin(m)+.0021*dsin(2*m)-.628*dsin(mprime)+.0089*dsin(2*mprime)-4e-4*dsin(3*mprime)+.0079*dsin(2*f)-.0119*dsin(m+mprime)-.0047*dsin(m-mprime)+3e-4*dsin(2*f+m)-4e-4*dsin(2*f-m)-6e-4*dsin(2*f+mprime)+.0021*dsin(2*f-mprime)+3e-4*dsin(m+2*mprime)+4e-4*dsin(m-2*mprime)-3e-4*dsin(2*m+mprime),pt+=phase<.5?.0028-4e-4*dcos(m)+3e-4*dcos(mprime):4e-4*dcos(m)-.0028-3e-4*dcos(mprime),apcor=!0),!apcor)throw"Error calculating moon phase!";return pt}function phasehunt(sdate,phases){var adate,k1,k2,nt1,nt2,yy,mm,dd,jyearResult=jyear(adate=sdate-45,yy,mm,dd);for(yy=jyearResult.year,mm=jyearResult.month,dd=jyearResult.day,adate=nt1=meanphase(adate,k1=Math.floor(12.3685*(yy+1/12*(mm-1)-1900)));nt2=meanphase(adate+=synmonth,k2=k1+1),!(nt1<=sdate&&nt2>sdate);)nt1=nt2,k1=k2;return phases[0]=truephase(k1,0),phases[1]=truephase(k1,.25),phases[2]=truephase(k1,.5),phases[3]=truephase(k1,.75),phases[4]=truephase(k2,0),phases}function kepler(m,ecc){var e,delta;e=m=toRad(m);do{e-=(delta=e-ecc*Math.sin(e)-m)/(1-ecc*Math.cos(e))}while(abs(delta)>epsilon);return e}function getMoonPhase(julianDate){var Day,N,M,Ec,Lambdasun,ml,MM,MN,Ev,Ae,MmP,mEc,lP,lPP,NP,y,x,MoonAge,MoonPhase,MoonDist,MoonDFrac,MoonAng,F,SunDist,SunAng;return N=fixAngle(360/365.2422*(Day=julianDate-epoch)),Ec=kepler(M=fixAngle(N+elonge-elongp),eccent),Ec=Math.sqrt((1+eccent)/(1-eccent))*Math.tan(Ec/2),Lambdasun=fixAngle((Ec=2*toDeg(Math.atan(Ec)))+elongp),F=(1+eccent*Math.cos(toRad(Ec)))/(1-eccent*eccent),SunDist=sunsmax/F,SunAng=F*sunangsiz,ml=fixAngle(13.1763966*Day+mmlong),MM=fixAngle(ml-.1114041*Day-mmlongp),MN=fixAngle(mlnode-.0529539*Day),MmP=MM+(Ev=1.2739*Math.sin(toRad(2*(ml-Lambdasun)-MM)))-(Ae=.1858*Math.sin(toRad(M)))-.37*Math.sin(toRad(M)),lPP=(lP=ml+Ev+(mEc=6.2886*Math.sin(toRad(MmP)))-Ae+.214*Math.sin(toRad(2*MmP)))+.6583*Math.sin(toRad(2*(lP-Lambdasun))),NP=MN-.16*Math.sin(toRad(M)),y=Math.sin(toRad(lPP-NP))*Math.cos(toRad(minc)),x=Math.cos(toRad(lPP-NP)),toDeg(Math.atan2(y,x)),NP,toDeg(Math.asin(Math.sin(toRad(lPP-NP))*Math.sin(toRad(minc)))),MoonAge=lPP-Lambdasun,MoonPhase=(1-Math.cos(toRad(MoonAge)))/2,MoonDist=msmax*(1-mecc*mecc)/(1+mecc*Math.cos(toRad(MmP+mEc))),MoonAng=mangsiz/(MoonDFrac=MoonDist/msmax),mparallax/MoonDFrac,{moonIllumination:MoonPhase,moonAgeInDays:synmonth*(fixAngle(MoonAge)/360),distanceInKm:MoonDist,angularDiameterInDeg:MoonAng,distanceToSun:SunDist,sunAngularDiameter:SunAng,moonPhase:fixAngle(MoonAge)/360}}function getMoonInfo(date){return null==date?{moonPhase:0,moonIllumination:0,moonAgeInDays:0,distanceInKm:0,angularDiameterInDeg:0,distanceToSun:0,sunAngularDiameter:0}:getMoonPhase(toJulianTime(date))}function getEaster(year){var previousMoonInfo,moonInfo,fullMoon=new Date(year,2,21),gettingDarker=void 0;do{previousMoonInfo=getMoonInfo(fullMoon),fullMoon.setDate(fullMoon.getDate()+1),moonInfo=getMoonInfo(fullMoon),void 0===gettingDarker?gettingDarker=moonInfo.moonIllumination<previousMoonInfo.moonIllumination:gettingDarker&&moonInfo.moonIllumination>previousMoonInfo.moonIllumination&&(gettingDarker=!1)}while(gettingDarker&&moonInfo.moonIllumination<previousMoonInfo.moonIllumination||!gettingDarker&&moonInfo.moonIllumination>previousMoonInfo.moonIllumination);for(fullMoon.setDate(fullMoon.getDate()-1);0!==fullMoon.getDay();)fullMoon.setDate(fullMoon.getDate()+1);return fullMoon}
Then run getEaster(2020); // -> Sun Apr 12 2020

Resources