Integer roots: Difference between revisions
Content deleted Content added
Added D |
Added Java |
||
Line 215: | Line 215: | ||
7 <.@%: (2*10x^2*2000) |
7 <.@%: (2*10x^2*2000) |
||
29619362959451736245702628695019269518064618216015009169507699742781423769947484925822512257735101524178182602734424986961003971858127002794053824818478879396020132662403256874761276690431037137165264232256601651438511207764019815767975124455844526943932927494896013055497926678521360177960529077012650088983239249505488961115547364229473827474458408002500739618874659540108997885564940730803150961523774615079827002013042942440654069714159530336055547627964891459096727426898214883744931710925020592035759639587602673656267343846153343265577563529779031634608306646526796</lang> |
29619362959451736245702628695019269518064618216015009169507699742781423769947484925822512257735101524178182602734424986961003971858127002794053824818478879396020132662403256874761276690431037137165264232256601651438511207764019815767975124455844526943932927494896013055497926678521360177960529077012650088983239249505488961115547364229473827474458408002500739618874659540108997885564940730803150961523774615079827002013042942440654069714159530336055547627964891459096727426898214883744931710925020592035759639587602673656267343846153343265577563529779031634608306646526796</lang> |
||
=={{header|Java}}== |
|||
{{trans|Kotlin}} |
|||
<lang Java>import java.math.BigInteger; |
|||
public class IntegerRoots { |
|||
private static BigInteger iRoot(BigInteger base, int n) { |
|||
if (base.compareTo(BigInteger.ZERO) < 0 || n <= 0) { |
|||
throw new IllegalArgumentException(); |
|||
} |
|||
int n1 = n - 1; |
|||
BigInteger n2 = BigInteger.valueOf(n); |
|||
BigInteger n3 = BigInteger.valueOf(n1); |
|||
BigInteger c = BigInteger.ONE; |
|||
BigInteger d = n3.add(base).divide(n2); |
|||
BigInteger e = n3.multiply(d).add(base.divide(d.pow(n1))).divide(n2); |
|||
while (!c.equals(d) && !c.equals(e)) { |
|||
c = d; |
|||
d = e; |
|||
e = n3.multiply(e).add(base.divide(e.pow(n1))).divide(n2); |
|||
} |
|||
if (d.compareTo(e) < 0) { |
|||
return d; |
|||
} |
|||
return e; |
|||
} |
|||
public static void main(String[] args) { |
|||
BigInteger b = BigInteger.valueOf(8); |
|||
System.out.print("3rd integer root of 8 = "); |
|||
System.out.println(iRoot(b, 3)); |
|||
b = BigInteger.valueOf(9); |
|||
System.out.print("3rd integer root of 9 = "); |
|||
System.out.println(iRoot(b, 3)); |
|||
b = BigInteger.valueOf(100).pow(2000).multiply(BigInteger.valueOf(2)); |
|||
System.out.print("First 2001 digits of the square root of 2: "); |
|||
System.out.println(iRoot(b, 2)); |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>3rd integer root of 8 = 2 |
|||
3rd integer root of 9 = 2 |
|||
First 2001 digits of the square root of 2: 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |