# What is the Sandbox?

This "Sandbox" is a place where Code Golf users can get feedback on prospective challenges they wish to post to the main page. This is useful because writing a clear and fully specified challenge on the first try can be difficult. There is a much better chance of your challenge being well received if you post it in the Sandbox first.

To post to the Sandbox, scroll to the bottom of this page or click on the "Add Proposal" link below, and click "Answer This Question". Click "OK" when it asks if you really want to add another answer. Write your challenge just as you would when actually posting it. You may also add some notes about specific things you would like to clarify before posting it. Other users will help you improve your challenge by rating and discussing it. When you think your challenge is ready for the public, go ahead and post it, replace the post here with a link to the challenge and delete it.

See the Sandbox FAQ for more information on how to use the Sandbox.

## Get the Sandbox Viewer to view the sandbox more easily

To add an inline tag to a proposal use shortcut link syntax with a prefix: [tag:king-of-the-hill]

## Traverse the Bridges of Köningsberg

The Seven Bridges of Köningsberg is a logical problem that singlehandedly kicked off both the fields of topology and graph theory. The city of Köningsberg was bisected by a river, with two islands in it. Thus the city spanned four landmasses. Connecting those were seven bridges. Leonhard Euler proved that it was impossible for a person to walk through Köningsberg and cross every bridge exactly once.

This is an increasingly abstract representation of the problem. The bridges can be represented as edges of a graph, and the landmasses as nodes. Try to start from one node, and "walk" to the other nodes, crossing every edge exactly once (crossing nodes multiple times is okay). Euler proved that it was impossible for Köningsberg. Info on how to solve this problem for any set of islands and bridges can be found on the Wiki page.

## The problem

As input, your program/function should take an adjacency matrix, in any form that you wish (e.g. concatenating every number to a single string is fine, as is making a string list, or even a built-in matrix data structure if your language has one). The examples here are provided using a csv format.

The adjacency matrix for Köningsberg looks like this:

0;2;1;2
2;0;1;0
1;1;0;1
2;0;1;0


Each row and column represents the bridges from and to specific nodes. Node 1 (first row) has 2 bridges to node 2 (second column), and vice versa. Every bridge is bi-directional, so the matrix will always be symmetrical. Bridges from a node to itself are allowed (that does not make much sense architecturally, but topology nerds recently hacked several city planning agencies to make this challenge more interesting, so do not disappoint them) - but by convention such connections are counted double in the adjacency matrix.

Output, for any given adjacency matrix, a truthey/falsey value for whether it is possible to walk so that you traverse every edge exactly once. You don't need to end up back at your starting position - that's a different problem. The maximum amount of nodes/landmasses is 9, and the maximum amount of bridges between two landmasses is also 9. The maximum amount of bridges from one landmass to itself is 4 (notated as 8 in the matrix). There is no guarantee that all the landmasses are connected - if there's islands that you cannot reach, but you can reach all the bridges, then the answer is still truthey!

This is a challenge, so the shortest challenge in bytes wins!

## Test cases

2


TRUE

2;8
8;2


TRUE

6;4;9
4;0;1
9;1;0


TRUE

6;2;4;0
2;4;3;9
4;3;2;3
0;9;3;4


TRUE

6;2;4;2;5
2;8;1;1;9
4;1;6;4;8
2;1;4;8;7
5;9;8;7;8


FALSE

0;0;0;0;0;0
0;0;0;0;0;0
0;0;0;0;0;0
0;0;0;0;0;0
0;0;0;0;0;0
0;0;0;0;0;0


TRUE (there's no bridges, so they can all be reached)

2;0;0;0;0;0;0
0;2;0;0;0;0;0
0;0;2;0;0;0;0
0;0;0;2;0;0;0
0;0;0;0;2;0;0
0;0;0;0;0;2;0
0;0;0;0;0;0;2


FALSE (every landmass only connects to itself)

0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0
0;0;0;0;0;0;0;0
0;0;0;0;0;1;0;0
0;0;0;0;1;0;1;0
0;0;0;0;0;1;0;1
0;0;0;0;0;0;1;0


TRUE (starting at a landmass with a bridge, you can reach all of them)

4;0;1;6;3;6;9;7;4
0;6;1;7;2;8;5;6;1
1;1;2;6;1;4;4;3;4
6;7;6;8;9;7;0;3;4
3;2;1;9;4;8;1;0;0
6;8;4;7;8;0;6;6;8
9;5;4;0;1;6;2;3;6
7;6;3;3;0;6;3;6;6
4;1;4;4;0;8;6;6;4


TRUE

## Sandbox

Do I need to include the logical solution to the problem? It's pretty simple, but I might want to make figuring that out part of the challenge.

Any other feedback welcome, of course.

• Why are the outputs to 5th and 6th examples False? Looks like 5th is invalid (because it's not symmetric) and 6th should be True (because there are no bridges to start with, so we already walked over all bridges). – Bubbler Dec 17 '19 at 4:30
• @Bubbler right on both counts (I was adjusting some of the squares but forgot the symmetry). Will update when I have time! – KeizerHarm Dec 17 '19 at 6:54
• @Bubbler fixed! Thank you! – KeizerHarm Dec 17 '19 at 8:24
• Pretty sure this is a duplicate – FlipTack Dec 20 '19 at 16:53
• @FlipTack Oh, bugger. Is this one different enough because the input is an adjacency matrix rather than a list of bridges? – KeizerHarm Dec 20 '19 at 19:11
• I wouldn't say so. Especially since the challenge isn't that interesting, just checking it's connected and there's 0-2 odd vertices. – FlipTack Dec 21 '19 at 6:55
• Especially because the degree of a node is simply the sum over its line in the adjacency matrix. – AlienAtSystem Dec 22 '19 at 16:52

# Input

The input will be a year between 1583 and 2250.

# Output

The Gregorian date of the first evening of Hannukah that year. That is the day before the first full day of Hannukah. Your code should output the month and day of the month in any easy to understand human readable form of your choice.

# Examples

2013    November 27
2014    December 16
2015    December 6
2016    December 24
2017    December 12
2018    December 2
2019    December 22
2020    December 10
2021    November 28
2022    December 18
2023    December 7
2024    December 25
2025    December 14
2026    December 4
2027    December 24
2028    December 12
2029    December 1
2030    December 20
2031    December 9
2032    November 27
2033    December 16


# How do you do this?

It could hardly be simpler. We start with a couple of definitions:

We define a new inline notation for the division remainder of $$\x\$$ when divided by $$\y\$$: $$(x|y)=x \bmod y$$

For any year Gregorian year $$\y\$$, the Golden Number, $$G(y) = (y|19) + 1$$ For example, $$\G(1996)=2\$$ because $$\(1996|19)=1\$$.

To find $$\H(y)\$$, the first evening of Hannukah in the year $$\y\$$, we need to find $$\R(y)\$$ and $$\R(y+1)\$$, the day of September where Rosh Hashanah falls in $$\y\$$ and in $$\y+1\$$. Note that September $$\n\$$ where $$\n≥31\$$ is actually October $$\n-30\$$.

$$R(y)=⌊N(y)⌋ + P(y)$$ where $$\⌊x⌋\$$ denotes $$\x-(x|1)\$$, the integer part of $$\x\$$, and

$$N(y)= \Bigl \lfloor \frac{y}{100} \Bigr \rfloor - \Bigl \lfloor \frac{y}{400} \Bigr \rfloor - 2 + \frac{765433}{492480}\big(12G(y)|19\big) + \frac{(y|4)}4 - \frac{313y+89081}{98496}$$

We define $$\D_y(n)\$$ as the day of the week (with Sunday being $$\0\$$) that September $$\n\$$ falls on in the year $$\y\$$. Further, Rosh Hashanah has to be postponed by a number of days which is

$$P(y)=\begin{cases} 1, & \text{if } D_y\big(\lfloor N(y)\rfloor \big)\in\{0,3,5\} & (1)\\ 1, & \text{if } D_y\big(\lfloor N(y)\rfloor\big)=1 \text{ and } (N(y)|1)≥\frac{23269}{25920} \text{ and } \big(12G(y)|19\big)>11 & (2)\\ 2, & \text{if } D_y\big(\lfloor N(y)\rfloor \big)=2 \text{ and } (N(y)|1)≥\frac{1367}{2160} \text{ and } (12G(y)|19)>6 & (3)\\ 0, & \text{otherwise} & (4) \end{cases}$$

For example, in $$\y=1996\$$, $$\G(y)=2\$$, so the $$\N(y)\approx13.5239\$$. However, since September 13 in 1996 was a Friday, by Rule $$\(1)\$$, we must postpone by $$\P(y)=1\$$ day, so Rosh Hashanah falls on Saturday, September 14.

Let $$\L(y)\$$ be the number of days between September $$\R(y)\$$ in the year $$\y\$$ and September $$\R(y+1)\$$ in year $$\y+1\$$.

The first evening of Hannukah is:

$$H(y)=\begin{cases} 83\text{ days after }R(y) & \text{if } L(y)\in\{355,385\}\\ 82\text{ days after }R(y) & \text{otherwise} \end{cases}$$

# Notes and thanks

Thank you to @Adám for pointing me to the rules. To keep things simple, this challenge assumes the location to be Jerusalem.

• Please type out the rules on that image into actual text. Challenges are supposed to be self-contained, while that image will be subject to link rot. Also, it's lacking an explanation how the Golden Number G is calculated. – AlienAtSystem Dec 29 '19 at 20:26
• What if the given year has no Hannukah? Or there are two "first day of Hannukah"s in the given year? – Adám Dec 30 '19 at 10:56
• @Adam. Now you have confused me! For which years between 1900 and 2100 were there 0 or 2 Hannukahs? – Anush Dec 30 '19 at 11:01
• @Anush Ah, I didn't notice the range. 3031 will have 0 and 3032 will have 2. – Adám Dec 30 '19 at 11:17
• Hold on, the first evening? That'd be the day before before the "first day of Hannukah". You should be very clear about this. – Adám Dec 30 '19 at 11:26
• Hm, I just noticed that there's a risk of people actually not implementing the algorithms, but instead relying on a built-in calendar conversion. Though you don't state it, the answer is always the 24th day of the month Kislev in the Hebrew year CivilYear+3761. – Adám Dec 30 '19 at 11:47
• Also, you may want to extend the valid input range. Otherwise it will often be shortest to hard-code the dates, e.g. in base 30. – Adám Dec 31 '19 at 14:31
• @Adam Could you please double check the formula in the question actually matches the example dates I have given. If so, I will post the question. – Anush Dec 31 '19 at 14:43
• Working on it... – Adám Dec 31 '19 at 15:00
• OK, I've tried it now and it works. It actually works. However, don't go ahead and post yet; There are a few issues to address. – Adám Dec 31 '19 at 15:39
• The first day of Hannukah day 84 if Rosh Hashanah is day 1, so you need to add 83 to get the first day of Hannukah, but 82 to get the evening before. – Adám Dec 31 '19 at 15:40
• The last three paragraphs are superfluous as your range is bigger. However, I also suggest adjusting the range somewhat. If you go above 2239 then .NET won't help (which may be good or bad depending on whether you want to push people to implement the actual algorithm instead of just converting the Hebrew date using the built-in. In any case, the Hebrew calendar isn't really defined beyond 2250. – Adám Dec 31 '19 at 15:47
• You can however pull back the earliest year to 1583, but not earlier, as that's the first year of the civil calendar. – Adám Dec 31 '19 at 15:49
• The mathematical formulas are really awkwardly written. Maybe MathJax those? I can do it if you want. – Adám Dec 31 '19 at 15:50
• I would adjust the output requirements to read "The Gregorian date of the first evening of Hannukah." to pre-empt people just submitting print("24 Kislev"). – AlienAtSystem Jan 1 at 8:24

Given a printable ASCII string separated with spaces, output the specified index of every word. E.g.

"are turbas unsafe ?!", 1


will yield run!

• When the index is out of bounds, this index should yield the null string (which can be joined with other strings).

## More test cases

"Is Pascal truly unloyal to users?",3 -> "sure"
"I'd pass kittens to anyone stopping by!!",4 -> "stop!"

• The definitive answer here is yes. – Lyxal Feb 3 at 6:58
• I assume this was just meant to be laying out the idea not to forget about it. But just in case I figured I should say that you definitely need to specify what happens when the index is too large for one of the substrings, and that the rest of the string will be printable ascii / whatever you choose. – FryAmTheEggman Feb 5 at 17:19
• The first test case in the more test cases section seem wrong should it not yield "sule" and not "sure"? – Mukundan314 Feb 11 at 15:56
• And the last test case only works for 1-indexing, while all others are 0-indexed. – AlienAtSystem Feb 11 at 16:10
• What happened to this user? – S.S. Anne Feb 11 at 23:58

# Is this entire list likewise-modulus-aligned?

A pair of numbers are aligned in a modulus when they all share the same remainder when they can be put under the modulus function against an integer greater than or equal to 2 and less than or equal to the absolute value of both.

For example,

13 and 22 are aligned numbers under 3 because
13%3 = 1
22%3 = 1

3<=13, 3<=22, and 3>=2

All our requirements are met.


A list is likewise-modulus-aligned when all the elements are aligned under the same modulus base.

## Challenge

Take in a list (not necessarily non-empty) of non-zero integers (not necessarily positive nor unique), and check if all the elements are likewise-modulus-aligned. Output is a truthy or falsy value.

Note; This is a "true-until-proven-otherwise" problem, meaning a single value in the list or an empty list will return TRUE.

## Example I/O

      In      | Out | Why
--------------|-----|---------
[5 7]|TRUE |1 mod 2
[7 12 18]|FALSE|(7,18) are not mod-aligned
[7 11 19]|TRUE |1 mod 2
[5 13 28 44]|FALSE|(5,28) are not mod-aligned
[10 13 37 108]|FALSE|(37,108) aren't aligned in any base below 10
[]|TRUE |No disproven pairs
[42]|TRUE |No disproven pairs
[-5 13 16]|TRUE |1 mod 3
[1 9 18]|FALSE|Arrays of size 2 or greater with 1 or -1 will always be false
[14 17 19]|FALSE|Every pair is modulus-aligned, but not under the same base
[17,22,32,107]|TRUE |2 mod 5
[4,8,12]|TRUE |0 mod 2
[-1,1]|FALSE|No mod 1 allowed
[1]|TRUE |No disproven pairs
[7,7,7]|TRUE |Numbers >=2 are always mod-aligned with themselves
[2,2,8,8]|TRUE |0 mod 2
[3 9 22 22]|FALSE|Pairs don't suddenly make (9,22) mod-aligned.


## Sandbox Questions

I'm gauging the interest to this question and seeing if this is an acceptable and unique challenge, just want to make sure I haven't missed another post doing a similar thing.

I changed the rules to be a lot more lenient on the comparisons, might do the pairwise comparison as a bonus or follow-up challenge later. But this is a compromise I can live with.

## Extra Hints/Tips

For all non-1 derivations, a number will be aligned with its negative self.
1 is never aligned with any other number, nor will -1.
A number that is a multiple of another will be aligned with that number in all its factors.
Numbers that share factors will always align, but numbers that don't share factors also may.
All odd numbers are aligned with each other, as are all even numbers.

• I'm not clear on why this challenge asks to compare all pairs of a list, rather than just a single pair. The condition covers pairs of numbers, so it would be more natural to just receive a pair of numbers as input. – isaacg Feb 19 at 18:21
• Because it's a combination of that function and a list-pair function with interesting shortcuts that can overlap. Asking for a pair could take X bits, and pushing all pairs to a function could take Y, but the combination of the two isn't necessarily X+Y. In my going at it, I saved like 8 bytes in the mix by being clever. This way, there are several ways to solve while still being a challenge. – Mathgeek Feb 19 at 18:24
• Some test cases that are not 1 mod x would be good – Jo King Feb 20 at 5:21
• Good suggestion, Jo. I just added one, and I'll add a few more in a bit - I hadn't even caught that! – Mathgeek Feb 20 at 13:32
• While what you observed can be true, in my experience it is not usually a source of interest in golfing to combine tasks unnecessarily. Similarly, does including negative numbers make this task more interesting? In most languages this will not make a difference, but it will make answering in some languages (consider Retina) substantially more difficult - to the point where people probably won't answer. I always try to recommend making a challenge as simple as possible - just like writing a proof. – FryAmTheEggman Feb 20 at 18:59
• I made negative numbers allowed because negative modulus is still a valid application of modulus - but that argument of "combining two things" is applicable to like literally every other code golf question. The string-wise calculus question could have just been "print out which of two characters is the largest", but then there was added difficulty to comparing pairs and assigning those values to distinct characters. This isn't just an arbitrary expansion, it's a setwise comparison of several elements applied over a function; You see these "expansions" all the time, so why is this one an issue? – Mathgeek Feb 20 at 19:24
• I agree that the part of the challenge where you check every pair seems unnecessary, and was going to comment on this independently. I think just having two numbers as inputs would make a better challenge overall. Or, have all numbers in the list need to be aligned by the same modulus, which seems like a more natural extension. – xnor Feb 20 at 19:33
• Okay, what about a fairer follow-up instead - instead of checking if all pairs are modulus-aligned, a list is modulus-aligned if all the entries share an identical mod-point. ie: They all have to be n mod m together for the list to be valid. Think that's still a more fair question? I think submitting pairs only is a very low-level simple problem that doesn't have any real puzzle or golfing elements to it, so I'd like it to be slightly more complex somehow. – Mathgeek Feb 20 at 19:37
• What you and xnor propose sounds good to me. I tried to phrase what I said as suggestions and opinions, because that is all they are. There isn't anything wrong with what you have, but I know I'd be more likely to answer if you changed some things about it. The same is true for many challenges on this site (including my own). Over time, I've come to see that including requirements that are technically valid but aren't necessary rarely adds interest to a challenge. – FryAmTheEggman Feb 20 at 20:29
• Doesn't [10 13 37 109] satisfy 1 mod 3 (and therefore it is modulus-aligned)? – Bubbler Feb 20 at 23:45
• You're correct fixed! – Mathgeek Feb 21 at 13:14
• @KevinCruijssen But that's fine. If I have a list [7, 14], they are aligned under Base 7. – Mathgeek Feb 21 at 14:40
• I also just read the "(not necessarily positive nor unique)" part of your challenge, so you might want to add a few test cases containing multiple of the same number in that case. – Kevin Cruijssen Feb 21 at 14:46
• Nice challenge btw. And good choice on explicitly mentioning "Note; This is a "true-until-proven-otherwise" problem, meaning a single value in the list or an empty list will return TRUE.", since those [1]/[-1] test cases are really annoying in my approach. ;) I had a prepared solution which worked for all initial test cases in 10 bytes, but now it's 50% larger to 15 bytes just to fix those two test cases, haha. Looking forward to when it goes live. I would leave it in the sandbox for a little while longer for others to give feedback though, just in case. Oh, and welcome to CGCC! – Kevin Cruijssen Feb 21 at 15:05
• @xnor yes, the question then dissolves to finding whether the gcd of the differences of consecutive elements is >1. – Don Thousand Feb 21 at 16:09

## Square-Cube Digit Usage

• In response to the first sentence: nice. – Lyxal Feb 20 at 0:43
• I'm not exactly sure how your title relates to the challenge. Obviously "Square-Cube Digit Usage" won't exactly roll off the tongue, but what you have now seems misleading. – FryAmTheEggman Feb 20 at 20:22
• Nice challenge. I prepared a solution for when it goes live. I would add a few more test cases first, though. One suggestion: 1333 (or 3133/3313/3331) -> 111 (first positive integer as input that has a 3-digit number as output). Here the results for the first 10,000 inputs. – Kevin Cruijssen Feb 21 at 10:53

# Draw an American flag for any amount of states

The flag of the United States of America goes by many names. The Stars and Stripes. Old Glory. The Last Known Non-Erotic Usage Of The Verb 'To Spangle'.

It is also one of the few flags semi-regularly updated. The red and white stripes represent the 13 original states, but one more star has been added to the blue canton for every state that joined the union later. This last happened in 1960, when Hawaii got in. Flag designs with 51 stars are already waiting for when Puerto Rico or Washington D.C. are made states, but this vexillologist is lazy. You are to make a program that can draw the flag with any number of stars desired!

### Specification

Here's a neat image of the official, government-standardised design for the current U.S. flag:

Disregard the contents of the canton for now. Your program must draw a flag that adheres to only the ratios I give here:

• A (the height of the flag) = 1
• B (the width of the flag) = 19/10
• C (the height of the canton) = 7/13
• D (the width of the canton) = 19/25
• L (the height of any stripe) = 1/13

Because raster solutions are not exact and this flag is commonly misdrawn anyway, there's tolerance of 2% for every ratio, taking the flag height as the base.

Furthermore, the correct colours must be used.

• Every odd-numbered stripe must be this shade (hex): #B22234
• The blue canton must be in this shade: #3C3B6E
• Every even-numbered stripe, and every star, must be in this shade: #FFFFFF

Conversions to other colour coordinate systems can be found on the wiki page as well.

### Stars

Your program must takes as input any integer between 0 and 200, and draw that number of stars within the canton. The following rules apply.

• Each star must have five outer points and be five-fold rotationally symmetrical.
• Each star must be the same size.
• The bounding circles of stars may overlap, but the surface of the stars itself may not overlap.
• The bounding circles of the stars may go outside the canton, but the surface of the stars itself may not go outside the canton.
• I don't want solutions that just place every star on the same line; that would leave a lot of blue canton untouched, which would be a waste. So, as a rule, the combined surface area of the bounding circles of every star in the canton must be at least 20% of the surface area of the canton.

Since overlapping bounding circles still count, you get a formula for the minimum width w of the star, where a is the area of the canton and n the number of stars: . See here for how it's derived.

### Other specifications

There's no minimum or maximum size for your output image, though I recommend something that will allow 200 stars to fit but still be demonstrably star-shaped. When they are only a few pixels high, it becomes hard to argue that they have the required amount of points. Obviously, for vector solutions any size is permissible.

This is , so the smallest program wins!

### Test cases

Because I gave no specific arrangement of the stars (you may arrange them however you want), there is an infinite number of correct and incorrect solutions for each number of stars. These are just examples of valid and invalid solutions:

Valid:

Invalid (stars too small):

Valid:

Invalid (stars of unequal size, going out of the canton):

Invalid (stars have too many points, stripes have wrong colours, colours are the wrong hue, proportions are wrong):

## Sandbox

Do I need more test cases? Any other feedback?

• If you really want to allow 0 as an input, you'll need an exception to the rule that the combined areas of the bounding circle must be at least 20% of the area of the canton. (If there aren't any stars, there aren't any bounding circles, so the combined areas would be 0.) – Mitchell Spector Mar 7 at 2:19
• I know it's less thematic, but maybe the task could be just to draw the canton? Arranging and drawing the stars is the interesting part, whereas the stripes aren't changing, so in terms of golfing the stripes seem somewhat extraneous. I guess you could also have the number of stripes be variable. – xnor Mar 7 at 17:40

## Symmetrical difference code-golf

Post'd.

• If a language supports it, can we take output and input as sets instead of a lists? – Chas Brown Apr 9 at 8:08
• I don't think it's a dupe. Post it. – HighlyRadioactive Apr 27 at 0:02

# Help, I've mixed my week up!

My dog ate my calendar, and now my days are all mixed up. I tried putting it back together, but I keep mixing up the days of the week! I need some help putting my calendar back together, with the days in the correct order.

And since I need my calendar put together as fast as possible, don't waste my time by sending me superfluous bytes. The fewer bytes I have to read, the better!

## Input

The days of the week, in any order. Input can be taken as a list of strings, or a space separated string, or any reasonable way of representing 7 strings (one for each day of the week).

The strings themselves are all capitalized, as weekdays should be, so the exact strings are:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday


## Output

The days of the week, in sorted order (Monday - Sunday). Output can be as a list of strings, or printed with some delimiter.

## Disclaimer

Note that this is a challenge, with the added benefit of being able to use the input to shorten your code. You are not required to use the input if you don't want to.

## Examples

To see example input and output, you can consult this python script.

## For the sandbox

• You cannot use 6 tags, and this still needs [code-golf]. Otherwise this seems to be a nice challenge. (I can see a 4-6 Jelly solution by sort-nth permutation though) – my pronoun is monicareinstate Apr 28 at 1:03
• @mypronounismonicareinstate I forgot about the code-golf tag, but of course it should be there. I have my own solution in MathGolf (not quite 4 bytes), but I'm interested in different approaches. – maxb Apr 28 at 6:19

# Fold my ACGT proteins code-golfstringbiologychemistry

Quoting Wikipedia, "Protein folding is the physical process by which a protein chain acquires its native 3-dimensional structure, a conformation that is usually biologically functional, in an expeditious and reproducible manner.". I don't know what that means but by means of a game called Foldit it seems we can use protein folding in some way to help and fight diseases.

Please bear in mind that the task described was inspired by the isolated meaning of the words in "protein folding" and doesn't necessarily translate into how protein folding really works! i.e. the title is just a pun.

# Implement the Polygamma function

The Polygamma function of order $$\m\$$, $$\\psi^{(m)}(z)\$$, is the $$\(m + 1)\$$th derivative of the logarithm of the gamma function, which is also the $$\m\$$th derivative of the digamma function. Your task is to take an integer $$\m\$$ and a positive real number $$\z\$$ and output $$\\psi^{(m)}(z)\$$

## Definitions

For those unfamiliar with the functions above (Gamma, Digamma and Polygamma), here are a few different definitions for each:

### $$\\Gamma(z)\$$

• The gamma function is an extension of the factorial ($$\x! = 1\cdot2\cdot3\cdots(x-1)\cdot(x)\$$) to real numbers
• $$\\Gamma(z) = \int_{0}^{\infty}x^{z-1}e^{-x}dx\$$
• $$\\Gamma(n) = (n - 1)! \:,\:\: n \in \mathbb{N}\$$
• $$\\Gamma(n+1) = n\Gamma(n) \:,\:\: n \in \mathbb{N}\$$\$

### $$\\psi(z)\$$

• The digamma function is the logarithmic derivative of the gamma function
• $$\\psi(z) = \frac{d}{dz}\ln(\Gamma(z))\$$
• $$\\psi(z) = \frac{\Gamma'(z)}{\Gamma(z)}\$$
• $$\\psi(z + 1) = \psi(z) + \frac{1}{z}\$$

### $$\\psi^{(m)}(z)\$$

• The polygamma function of order $$\m\$$ is the $$\m\$$th derivative of the digamma function
• $$\\psi^{(m)}(z) = \frac{d^m}{dz^m}\psi(z)\$$
• $$\\psi^{(m)}(z) = \frac{d^{m+1}}{dz^{m+1}}\ln(\Gamma(z))\$$
• $$\\psi^{(m)}(z+1)= \psi^{(m)}(z) + (-1)^m\frac{m!}{z^{m+1}}\$$

You are to take two inputs, a natural number $$\m\$$ and a positive real number $$\z\$$, and output $$\\psi^{(m)}(z)\$$. The inputs and outputs will always fit within the number bounds of your language, but your algorithm must work theoretically for any and all inputs.

As the output is usually going to be a real number, rather than an integer, the output should show at least 5 decimal places. This may be optionally ignored if the output is an integer, but not if the decimal part of the output begins with .00000. For example, $$\\psi^{(6)}(20) = -0.000002172607350\$$, so an output of $$\-0.00000\$$ is acceptable, but $$\0\$$ is not. Note that the sign is required.

This is so the shortest code in bytes wins.

## Test cases

Results may differ due to floating point inaccuracies, Python's scipy library was used to generate the values. Values are rounded to 15d.p., unless otherwise stated.

 m,                  z -> ψ⁽ᵐ⁾(z)
17,                  2 -> 1357763223.715975761413574
5,                 40 -> 0.000000249389435
9,           53.59375 -> 0.000000000012010
35,                  9 -> 469354.958166260155849
46,                  5 -> -7745723758939047727202304.000000000000000
7, 1.2222222222222222 -> 1021.084176496877490
28,               6.25 -> -2567975.924144014250487
2,               7.85 -> -0.018426049840992


This table has the values of $$\\psi^{(m)}(z)\$$ for $$\0 \le m \le 9\$$ and $$\1 \le z \le 20\$$:


+---+------------------------+---------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+
|   |           1            |          2          |         3          |         4          |         5          |         6          |         7          |         8          |         9          |         10         |         11         |         12         |         13         |         14         |         15         |         16         |         17         |         18         |         19         |         20         |
+---+------------------------+---------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+
| 0 |   -0.577215664901533   |  0.422784335098467  | 0.922784335098467  | 1.256117668431800  | 1.506117668431800  | 1.706117668431800  | 1.872784335098467  | 2.015641477955610  | 2.140641477955610  | 2.251752589066721  | 2.351752589066721  | 2.442661679975812  | 2.525995013309145  | 2.602918090232222  | 2.674346661660794  | 2.741013328327460  | 2.803513328327460  | 2.862336857739225  | 2.917892413294781  | 2.970523992242149  |
| 1 |   1.644934066848227    |  0.644934066848227  | 0.394934066848226  | 0.283822955737115  | 0.221322955737115  | 0.181322955737115  | 0.153545177959338  | 0.133137014694031  | 0.117512014694031  | 0.105166335681686  | 0.095166335681686  | 0.086901872871768  | 0.079957428427324  | 0.074040268664010  | 0.068938227847684  | 0.064493783403239  | 0.060587533403239  | 0.057127325790783  | 0.054040906037696  | 0.051270822935203  |
| 2 |   -2.404113806319188   |  -0.404113806319189 | -0.154113806319189 | -0.080039732245115 | -0.048789732245114 | -0.032789732245115 | -0.023530472985855 | -0.017699569195768 | -0.013793319195768 | -0.011049834970802 | -0.009049834970802 | -0.007547205368999 | -0.006389797961592 | -0.005479465690312 | -0.004750602716552 | -0.004158010123959 | -0.003669728873959 | -0.003262645625435 | -0.002919710097314 | -0.002628122402315 |
| 3 |   6.493939402266829    |  0.493939402266829  | 0.118939402266829  | 0.044865328192755  | 0.021427828192755  | 0.011827828192755  | 0.007198198563125  | 0.004699239795945  | 0.003234396045945  | 0.002319901304290  | 0.001719901304290  | 0.001310093231071  | 0.001020741379219  | 0.000810664701232  | 0.000654479778283  | 0.000535961259764  | 0.000444408525389  | 0.000372570305061  | 0.000315414383708  | 0.000269374221340  |
| 4 |  -24.886266123440890   |  -0.886266123440879 | -0.136266123440878 | -0.037500691342113 | -0.014063191342113 | -0.006383191342113 | -0.003296771589026 | -0.001868795150638 | -0.001136373275638 | -0.000729931168235 | -0.000489931168235 | -0.000340910050701 | -0.000244459433417 | -0.000179820455575 | -0.000135196191875 | -0.000103591253604 | -0.000080703070010 | -0.000063799959344 | -0.000051098643488 | -0.000041405977726 |
| 5 |  122.081167438133861   |  2.081167438133896  | 0.206167438133897  | 0.041558384635954  | 0.012261509635954  | 0.004581509635954  | 0.002009493175049  | 0.000989510004771  | 0.000531746332896  | 0.000305945162117  | 0.000185945162117  | 0.000118208290511  | 0.000078020533309  | 0.000053159387985  | 0.000037222150950  | 0.000026687171526  | 0.000019534614153  | 0.000014563111016  | 0.000011034967722  | 0.000008484266206  |
| 6 |  -726.011479714984489  |  -6.011479714984437 | -0.386479714984435 | -0.057261607988551 | -0.013316295488551 | -0.004100295488551 | -0.001528279027645 | -0.000654007738836 | -0.000310684984930 | -0.000160150871077 | -0.000088150871077 | -0.000051203486564 | -0.000031109607963 | -0.000019635233198 | -0.000012804988755 | -0.000008590996985 | -0.000005908787970 | -0.000004154139804 | -0.000002978092040 | -0.000002172607350 |
| 7 |  5060.549875237640663  |  20.549875237639476 | 0.862375237639470  | 0.094199654649073  | 0.017295357774073  | 0.004392957774073  | 0.001392271903016  | 0.000518000614207  | 0.000217593204539  | 0.000100511115987  | 0.000050111115987  | 0.000026599144024  | 0.000014877714841  | 0.000008699205352  | 0.000005284083130  | 0.000003317553637  | 0.000002144087193  | 0.000001421585007  | 0.000000964233099  | 0.000000667475582  |
| 8 | -40400.978398747647589 | -80.978398747634884 | -2.228398747634885 | -0.179930526327158 | -0.026121932577158 | -0.005478092577158 | -0.001477178082416 | -0.000478010895205 | -0.000177603485537 | -0.000073530517936 | -0.000033210517936 | -0.000016110901963 | -0.000008296615840 | -0.000004494456155 | -0.000002542957742 | -0.000001494142013 | -0.000000907408791 | -0.000000567407762 | -0.000000364140247 | -0.000000239189714 |
| 9 | 363240.911422382690944 | 360.911422382626938 | 6.536422382626807  | 0.391017718703625  | 0.044948382766125  | 0.007789470766125  | 0.001788099024012  | 0.000503455497598  | 0.000165497161722  | 0.000061424194120  | 0.000025136194120  | 0.000011145599233  | 0.000005284884641  | 0.000002652620244  | 0.000001398085550  | 0.000000768796112  | 0.000000438758675  | 0.000000258758130  | 0.000000157124373  | 0.000000097937278  |
+---+------------------------+---------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+

• Mathematica: PolyGamma`. – my pronoun is monicareinstate Jul 27 at 3:37
• Looks good, except that I have no idea where to start. – Bubbler Jul 31 at 1:00