Code Golf Asked by user9207 on October 4, 2020
This task takes no input.
Your code should compute and print (or return)
$pi^y$ for all $y = frac11, frac12, frac13, frac14, frac15, frac16, frac17, frac18, frac19, frac1{10}, frac1{11}, frac1{12}$. The first 20 digits of the output (truncated and without any rounding) should be correct in each case.
Here are the answers:
3.1415926535897932384
1.7724538509055160272
1.4645918875615232630
1.3313353638003897127
1.2572741156691850593
1.2102032422537642759
1.1776640300231973966
1.1538350678499894305
1.1356352767378998683
1.1212823532318632987
1.1096740829646979321
1.1000923789635869829
$n=1;print substr(Math::BigFloat->bpi()**(1/$n++),0,21).$/ while($n<13);
Even using $pi$ from BigFloat, it doesn't seem to exactly match the given outputs. I'm sure it's truncated properly, though.
Answered by Razetime on October 4, 2020
print*,(acos(-1._16)**(1/real(i,16)),i=1,12)
end
Just an implicit loop with quad-precision vars. Not transferable, but works with GCC.
Answered by DeathIncarnate on October 4, 2020
This uses __float128
to get the required precision.
#import<quadmath.h>
s[9];main(i){for(;i<13;puts(s))quadmath_snprintf(s,36,"%.99Qf",expq(logq(M_PIq)/i++));}
I was curious to try this using the GNU MPFR library too:
#include<mpfr.h>
i;main(){MPFR_DECL_INIT(p,99);for(;i<12;){mpfr_const_pi(p,99);mpfr_root(p,p,++i,MPFR_RNDN);mpfr_printf("%.19Rfn",p);}}
Answered by Digital Trauma on October 4, 2020
Requires package Rmpfr.
mpfr("1.00000000238982476721842284052415179434",30 *log2(10))^(479001600/(1:12))
Gives the following, with truncations mine and some minor formatting
12 'mpfr' numbers of precision 99 bits
3.1415926535897932384...
1.7724538509055160272...
1.4645918875615232630...
1.3313353638003897127...
1.2572741156691850593...
1.2102032422537642759...
1.1776640300231973966...
1.1538350678499894305...
1.1356352767378998683...
1.1212823532318632987...
1.1096740829646979321...
1.1000923789635869829...
See below for why simpler versions don't work. Direct calculation using exponents 1/N for N=1,12 returned inaccurate value fairly early. I figured that was probably due to R or Rmpfr rounding 1/3 early, so whole number exponents would be preferred. So I calculated pi^(12!) (12!=479001600) using Mathematica, then raised it to the power of 12!/N, which would always be a whole number. I had to further tune it by passing the number to Rmpfr as a character vector (so R wouldn't round it), and by using an arbitrarily high precision in both Mathematica and Rmpfr so it would truncate accurately. Because of those arbitrary additions, I can probably shave off a few bytes, but I'm good with it as is.
This only works if R value for pi is accurate, which it isn't. Even reassigning the variable pi to a more accurate representation does not improve accuracy, as it rounds or something around 17 decimals.
format(pi^(1/1:12),nsmall=20)
Or, for 30 bytes
options(digits=20);pi^(1/1:12)
There's a package that gives a more accurate value for pi and other floating point numbers, Rmpfr, which you'll find referenced in questions about pi in R. One might expect the following to give the desired output.
library(Rmpfr)
Const("pi",20 *log2(10))^(1/1:12)
It doesn't. It gives
12 'mpfr' numbers of precision 66 bits
[1] 3.1415926535897932385 1.7724538509055160273 1.464591887561523232
[4] 1.3313353638003897128 1.2572741156691850754 1.2102032422537642632
[7] 1.177664030023197386 1.1538350678499894305 1.1356352767378998604
[10] 1.1212823532318633058 1.1096740829646979353 1.1000923789635869772
This is wrong on all counts by rounding or being a few off in the last digits (sidenote: the rnd.mode flag for mpfr does not fix this). Now one might think if we went up to many digits (say 100), then it would surely be correct to the first 20 digits. Nope
12 'mpfr' numbers of precision 332 bits
[1] 3.1415926535897932384...
[2] 1.7724538509055160272...
[3] 1.4645918875615232319...
[4] 1.3313353638003897127...
[5] 1.2572741156691850753...
[6] 1.2102032422537642631...
[7] 1.1776640300231973859...
[8] 1.1538350678499894305...
[9] 1.1356352767378998603...
[10] 1.1212823532318633058...
[11] 1.1096740829646979353...
[12] 1.1000923789635869771...
(Truncations mine). These don't all match OP or the other responses.
Answered by John on October 4, 2020
-lm
), Shaved off 40 49 bytes thanks to ceilingcat.
i;p(){for(;i<12;)printf("%.14f%d ",pow(acos(~fesetround(1024)),1./++i),L"纀罶?얡꜇???胛??"[i]);}
Output:
3.1415926535897932384 1.7724538509055160272 1.4645918875615232630 1.3313353638003897127 1.2572741156691850593 1.2102032422537642759 1.1776640300231973966 1.1538350678499894305 1.1356352767378998683 1.1212823532318632987 1.1096740829646979321 1.1000923789635869829
Answered by S.S. Anne on October 4, 2020
vpa(pi,22).^(1./(1:12))
Declares a variable precision arithmetic (VPA) pi. Octave then cleverly infers that the double constant pi
actually means pi, not whatever the double constant contains.
Answered by Sanchises on October 4, 2020
v->Math.PI+"2384 1.7724538509055160272 1.4645918875615232630 1.3313353638003897127 1.2572741156691850593 1.2102032422537642759 1.1776640300231973966 1.1538350678499894305 1.1356352767378998683 1.1212823532318632987 1.1096740829646979321 1.1000923789635869829"
-40 bytes by just hard-coding the output instead of calculating it.. >.>
-9 bytes thanks to @ceilingcat.
Old 299 291 bytes answer with actual calculations..
v->{var P=new java.math.BigDecimal(Math.PI+"2384");var x=P;for(int i=0;++i<13;System.out.println(x)){var p=P;for(x=P.divide(P.valueOf(i),21,5);x.subtract(p).abs().compareTo(P.ONE.movePointLeft(22))>0;)x=P.valueOf(i-1).multiply(p=x).add(P.divide(x.pow(i-1),21,5)).divide(P.valueOf(i),21,5);}}
Not entirely precise, but good enough to have the first 20 digits correct.
Explanation:
Most bytes come from the fact that BigDecimal
doesn't have a builtin for BigDecimal.pow(BigDecimal)
nor the $n$th root, so we'll have to calculate this manually..
v->{ // Method with empty unused parameter and no return-type
var P=new java.math.BigDecimal(Math.PI+"2384");
// Create a BigDecimal for PI
var x=P; // Create a BigDecimal to print after every iteration
for(int i=0;++i<13; // Loop `i` in the range [1,12]:
System.out.println(x)){// After every iteration: print `x` to STDOUT
var p=P; // Create a BigDecimal to save the previous value
for(x=P.divide(P.valueOf(i),
// Set `x` to PI divided by `i`
21,5); // (with precision 21 and rounding mode HALF_DOWN)
// Continue an inner loop as long as:
x.subtract(p).abs() // The absolute difference between `x` and `p`
.compareTo(P.ONE.movePointLeft(22))>0;)
// is larger than 1e-22
x= // Set `x` to:
P.valueOf(i-1) // `i-1`
.multiply(p=x) // Multiplied by `x` (and store the previous `x` in `p`)
.add( // And add:
P.divide( // PI divided by
x.pow(i-1),21,5)) // `x` to the power `i-1`
.divide(P.valueOf(i),21,5);}}
// Divided by `i`
Answered by Kevin Cruijssen on October 4, 2020
Variant of Jitse's answer, who wants to stick to standard libraries. As mentioned by @Seb & @Jitse, Rational
or E/E
are needed because 1/i
isn't precise enough as float.
from sympy import*;i=E/E
while i<13:print(N(pi**(1/i),99));i+=1
As a bonus, sympy
allows to output 99 decimals with the same byte count as for 20 decimals:
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211707
1.77245385090551602729816748334114518279754945612238712821380778985291128459103218137495065673854467
1.46459188756152326302014252726379039173859685562793717435725593713839364979828626614568206782035382
1.33133536380038971279753491795028085330936622381810425845370748286670076101723561496824589105670695
1.25727411566918505938452211411044829390616631003965817353418162262716270074393519788994784267497245
1.21020324225376427596603076175994353105276255513631810467305643780646240044814351479113420061960303
1.17766403002319739668470085583704641096763145003350008756991802723553566109741289228117335907046316
1.15383506784998943054096521314988190017738887987082462087415032873413337243208073328092932090341440
1.13563527673789986837981146453309184806238366027985302804182074656192075351096762716281170420998022
1.12128235323186329872203095522015520934269381296656043824699758762377264153679046528721553289724806
1.10967408296469793211291254568401340513968966612239757917494488030006513039871082996506101420959919
1.10009237896358698298222864697784031503277459309498017808547779493972810083028975298933600450861044
Answered by Eric Duminil on October 4, 2020
-rbigdecimal/math
, Conveniently, even though BigMath.PI(9)
only guarantees precision up to 9 digits, it actually is precise up to 26, which is enough to calculate the exponents, which use the builtin Rational
fractions instead of floats to ensure the precision is still good enough. (Also, making a Rational
saves a byte over dividing it normally, since Ruby uses integer division if both arguments are integers, necessitating the use of 1.0
somewhere in the code.)
-15 bytes from histocrat!
1.upto(12){|i|puts BigMath.PI(9).**(1r/i).to_s ?F}
Answered by Value Ink on October 4, 2020
12İ€ØP*×ȷ19Ḟ+“Ṿẏ⁽)¬ọƓỴ³ɲỊUị&ıİḣl’ḃ4ȷ¤_2ȷD;"€”.
Similar to some other answers, but encodes the difference between the Jelly answer and the correct answer (with big integer arithmetic). Full explanation to follow.
Answered by Nick Kennedy on October 4, 2020
import math
for i in range(1,13):print(str(math.pi**(1/i))[:~(i==2)]+str([32384,60272,2630,7127,'0593',2759,3966,4305,8683,2987,9321,69829][i-1]))
This uses a similar method to Arnauld's JavaScript solution, by using the inbuilt pi value with the extra precision added to the end.
Answered by Matthew Jensen on October 4, 2020
#1&"3.1415926535897932384
1.7724538509055160272
1.4645918875615232630
1.3313353638003897127
1.2572741156691850593
1.2102032422537642759
1.1776640300231973966
1.1538350678499894305
1.1356352767378998683
1.1212823532318632987
1.1096740829646979321
1.1000923789635869829"
There is no way to get more precision out of floats in cQuents, so the values must be hardcoded as strings.
#12&`p^(1/$
Does not reach the required precision levels.
Answered by Stephen on October 4, 2020
Answered by Digital Trauma on October 4, 2020
6«{“≥αyHT.─C¹„1.0000ŗ┤“^m„┘÷^]
Don't try it here! It'll take a while to calculate all those digits (it took ~15 minutes to run for me). Rather, here's a version of the same code, only outputting the last 3 items.
Computes C ^ (27720/N)
where C
is the hard-coded constant pi^(1/27720)
= 1.000041297024626834690309
and N is looped over from 1 to 12. The big number library decided to expand the amount of significant digits for successively bigger N, making the code take unreasonable amounts of time to run.
Answered by dzaima on October 4, 2020
@.π^(1/big(1:12))
(1./big(1:12))
divides a BigInt
1 by each of 1 thru 12, then π.^
raises pi to each of those values. So long as there is one BigInt
or BigFloat
involved in each computation, it will calculate the result at that precision. The @.
macro transforms the code to add dots to every function call (thus the dots that appear in my explanation that don't appear in the code snippet), this causes it to "broadcast" which for this purpose means do it all elementwise.
20->19 thanks to Robin Ryder
19->18 thanks to TimD
Answered by gggg on October 4, 2020
following @ChristianSievers comment |cut -c -21
could be removed.
-3 bytes thanks to @DigitalTrauma.
echo "e(l(4*a(1))/"{1..12}");"|bc -l
Some explanations
bc -l
define math functions and set scale to 20, see man bc
for more detailsa()
atan function, so 4*a(1)
is pi
e()
exp functionl()
log function, so e(l(x)/y)
is x^(1/y)
Answered by Nahuel Fouilleul on October 4, 2020
-2 bytes thanks to game0ver -1 byte thanks to LegionMammal978
Pi^(1`11/Range@12)&
Answered by Galen Ivanov on October 4, 2020
vector(12,n,Pi^n^-1)
(TIO needs some supporting code, but this works as is in the REPL.
Answered by Christian Sievers on October 4, 2020
•u¬Œo¡õ≠ÎĆζw1Á4¼©éßw–xùó1O•5ôεžqN>zm16£ì
-7 bytes thanks to @Grimy.
Uses the legacy version, because for some reason the new version outputs $1.0$ for $pi^{frac{1}{1}}$.. :S
Explanation:
•u¬Œo¡õ≠ÎĆζw1Á4¼©éßw–xùó1O•
# Push compressed integer 323846027232630971275059342759739669430598683329877932169829
5ô # Split it into parts of size 5:
# [32384,60272,32630,97127,50593,42759,73966,94305,98683,32987,79321,69829]
ε # Then map each integer to:
N> # Take the 0-based map-index, and increase it by 1
z # Calculate 1/(index+1)
žq m # Then calculate PI the power of this
16£ # Only leave the first 16 characters (including decimal dot)
ì # And prepend it before the current integer we're mapping
# (after which the mapped result is output implicitly)
See this 05AB1E tip of mine (sections How to compress large integers?) to understand why •u¬Œo¡õ≠ÎĆζw1Á4¼©éßw–xùó1O•
is 323846027232630971275059342759739669430598683329877932169829
.
Answered by Kevin Cruijssen on October 4, 2020
import fractions as f;p=f.Decimal('%s2384'%f.math.pi);i=p/p
while i<13:print(p**i**-1);i+=1
-23 bytes thanks to flornquake
Answered by Jitse on October 4, 2020
Computes as many digits as the precision of IEEE 754 allows and hardcodes the other ones.
_=>[32384,60272,2630,7127,[n=0]+593,2759,3966,4305,8683,2987,9321,69829].map(v=>(Math.PI**(1/++n)+'').slice(0,~(n==2))+v)
_ => // input is ignored
[ 32384, 60272, 2630, 7127, // hard-coded digits for n=1 to n=4
[n = 0] + 593, // initialize n to 0, and set this entry to '0593' (n=5)
2759, 3966, 4305, 8683, // hard-coded digits for n=6 to n=9
2987, 9321, 69829 // hard-coded digits for n=10 to n=12
].map(v => // for each entry in the above array:
(Math.PI ** (1 / ++n) + '') // increment n; compute π**(1/n) and coerce it to a string
.slice(0, ~(n == 2)) // remove the last 2 digits if n=2,
// or only the last digit otherwise
+ v // append the hard-coded digits
) // end of map()
Answered by Arnauld on October 4, 2020
(⍳12)√○1
Try it online! (⎕PP←34
is Print Precision: 34 digits; ⎕FR←1287
is Float Representation: 128-bit decimal)
○1
$π×1$
(
…)√
take the following roots of that:
⍳12
ɩndices 1 through 12
Answered by Adám on October 4, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP