Calculate a pi-based sequence with a pi-based frequence - Code Golf Stack Exchange most recent 30 from codegolf.stackexchange.com 2019-11-23T00:19:44Z https://codegolf.stackexchange.com/feeds/question/41178 https://creativecommons.org/licenses/by-sa/4.0/rdf https://codegolf.stackexchange.com/q/41178 7 Calculate a pi-based sequence with a pi-based frequence Ypnypn https://codegolf.stackexchange.com/users/16294 2014-11-06T21:06:58Z 2014-11-07T05:41:19Z <h2>Introduction</h2> <p>Pi is</p> <pre><code>3.1415926535897932384626433832795028841971693993751058209749445923078164062862089... </code></pre> <p>Let's treat each digit as an instruction as to how to get to the next digit.</p> <ul> <li>The first digit is 3.</li> <li>Counting three digits after the first digit, we arrive at 1.</li> <li>One digit after this 1 is a 5.</li> <li>Five digits after that is 3.</li> <li>Three digits later is 9.</li> </ul> <p>The digits included in this sequence are in bold: <strong>3</strong>.14<strong>15</strong>9265<strong>3</strong>58<strong>9</strong>79323846<strong>2</strong>6<strong>4</strong>33...</p> <p>If a digit is 0, the next digit is 10 later.</p> <h2>Challenge</h2> <p>Given a positive integer <code>n</code> less than 1000, find out how many of the first <code>n</code> digits of pi, including the initial 3, are in the sequence.</p> <p>Furthermore, your code's characters must have a "pi-based frequency". Choose any character; your program must use exactly three of that character. Choose another character, use exactly one of it. Choose another character, use it four times. You may continue for as long as you wish, but each character chosen must be different.</p> <p>For example, the following snippet complies with the frequency rules (but produces the wrong output):</p> <pre><code>print "r\"rriiiittttnttttppppp" # Uses 3 quotation marks, 1 space, 4 r's, 1 \, 5 i's, 9 t's, 2 n's, and 6 p's. </code></pre> <p>For the purposes of "pi-based frequency", treat the digit 0 as 10.</p> <p>You may write a function or a full program. You may not use any built-in constants, trigonometry functions, or complex number functions to find pi.</p> <p>Shortest code in characters wins. The tiebreaker is the most votes.</p> <h2>Examples</h2> <pre><code>Input Output ------------- 1 1 2 1 3 1 4 2 5 3 28 8 77 17 123 28 328 73 625 122 999 189 </code></pre> https://codegolf.stackexchange.com/questions/41178/-/41180#41180 4 Answer by Martin Ender for Calculate a pi-based sequence with a pi-based frequence Martin Ender https://codegolf.stackexchange.com/users/8478 2014-11-06T22:46:40Z 2014-11-06T23:02:32Z <h2>Mathematica, 253 bytes</h2> <pre><code>f=(For[i=0;j=1,j&lt;=#,j+=Mod[RealDigits[9801/Sqrt@8/Sum[(4j)!(1103+26390j)/(j!)^4/396^(4j),{j,0,125}],10,999][[1,j]]-1,10]+1;++i];"99iiiitS===oooo8862^^^^^^\uusRRRRRRqqqqqqqqMMMMmlllllllgggggggFFFeeeeeeeeDDDDDDaaaaa55555555&lt;&lt;{{\{{{{{{}}}}}}}}--@@@@@@";i)&amp; </code></pre> <p>Ungolfed:</p> <pre><code>f = ( For[i = 0; j = 1, j &lt;= #, j += Mod[ RealDigits[ 9801/Sqrt@8/ Sum[(4 j)! (1103 + 26390 j)/(j!)^4/396^(4 j), {j, 0, 125}], 10, 999][[1, j]] - 1, 10] + 1; ++i]; "99iiiitS===oooo8862^^^^^^uusRRRRRRqqqqqqqqMMMMmlllllllgggggggFFFeeeeeeeeDDDDDDaaaaa55555555&lt;&lt;{{{{{{{{}}}}}}}}--@@@@@@"; i ) &amp; </code></pre> <p>Usage is <code>f</code>.</p> <p>A whopping 122 bytes are used to pad the code with a useless string to get the right character frequencies. I'll try to improve that tomorrow.</p> <p>The character frequencies should match the digits of pi in this order:</p> <pre><code>3#/&amp;(,![)4+9j0it"S=;]ro862^usRqM1mlgFfeDda5&lt;{}-@ </code></pre> <p>I confirmed that there is <em>some</em> correct order with the following snippet:</p> <pre><code>Sort[Last /@ Tally[Characters@ "f=(For[i=0;j=1,j&lt;=#,j+=Mod[RealDigits[9801/Sqrt@8/Sum[(4j)!(\ 1103+26390j)/(j!)^4/396^(4j),{j,0,125}],10,999][[1,j]]-1,10]+1;++i];\"\ 99iiiitS===oooo8862^^^^^^\ uusRRRRRRqqqqqqqqMMMMmlllllllgggggggFFFeeeeeeeeDDDDDDaaaaa55555555&lt;&lt;{{\ {{{{{{}}}}}}}}--@@@@@@\";i)&amp;"]] == Sort[RealDigits[Pi, 10, 48][] /. 0 -&gt; 10] </code></pre> <p>I'm computing pi with <a href="http://en.wikipedia.org/wiki/Approximations_of_%CF%80#20th_century" rel="nofollow">Ramanujan's series</a>. It converges to 1000 digits in 125 terms. Due to golfing reasons, I recompute the 999 necessary digits for every single digit of the subsequence, but it still completes within a second for <code>n = 999</code> on my machine.</p> https://codegolf.stackexchange.com/questions/41178/-/41198#41198 4 Answer by Display_name for Calculate a pi-based sequence with a pi-based frequence Display_name https://codegolf.stackexchange.com/users/32602 2014-11-07T02:34:21Z 2014-11-07T05:41:19Z <h1>C++, <strike>550</strike> 428</h1> <pre><code>#include&lt;iostream&gt; long a,b,c=3505,d,e,f=10000,g,h,i,j,k,l,x,z;int main(){int P;int M;for(;(b=c-=14)&gt;0;){for(;--b&gt;0;){d*=b;if(h==0)d+=2000*f;else d+=a[b]*f;g=b+b-1;a[b]=d%g;d/=g;}h=e+d/f;i=h;for(j=0;j&lt;4;j++){M[j]=i%10;i/=10;}for(j=3;j&gt;=0;j--){P[z]=M[j];z++;};d=e=d%f;}std::cin&gt;&gt;k;j=0;while(j&lt;k){x=P[j];(x==0)?x=10:x;j+=x;l++;}std::cout&lt;&lt;l;}//{{{:+-2220000%,ddhhhhiiiiiiijjnorrt, # # # ,MQQQRRRSSSSSSSSTTTTTTTTT:}}} </code></pre> <p>I'm not 100% confident that my code has a π-based frequence. I will check tomorrow. If it works, there is a direct correspondance between the frequencies of the following characters and the 60 first digits of π.</p> <pre><code> zdj(mxakcb)1[*uPes&gt;0M34t;l]g+=n%wo-,hQ{}Rr&lt;f/Si :sT#2 3.141592653589793238462643383279502884197169399375105820974944 </code></pre> <p>The craziest part is the computation of π. It is done thanks to an implementation of <a href="http://www.cecm.sfu.ca/~jborwein/Expbook/Manuscript/Related%20files/spigot.pdf" rel="nofollow noreferrer">a fascinating spigot algorithm</a>:</p> <p><img src="https://i.stack.imgur.com/IdgK0.png" alt="enter image description here"></p> <blockquote> <p><strong>Edit:</strong> you can generate <em>n</em> digits of π with this 288 characters-long program: :)</p> </blockquote> <pre><code>#include&lt;iostream&gt; #include&lt;vector&gt; using namespace std;long b,c,d,e,f=10000,g,h,l;int main(){cout&lt;&lt;"? ";cin&gt;&gt;l;cout&lt;&lt;": ";l=(l/4+1)*14;c=l;vector&lt;long&gt; a;a.resize(l);for(;(b=c-=14)&gt;0;){for(;--b&gt;0;){d*=b;if(h==0)d+=2000*f;else d+=a[b]*f;g=b+b-1;a[b]=d%g;d/=g;}h=e+d/f;cout&lt;&lt;h;d=e=d%f;}} </code></pre>