Integer roots: Difference between revisions
Content deleted Content added
m →integer result only: elided a duplicated line (a cut 'n paste finger-check). |
Go solution |
||
Line 8: | Line 8: | ||
Example: With N=3 and X=9 you would again calculate the number 2 because 2 is the largest integer less than or equal to the root R. |
Example: With N=3 and X=9 you would again calculate the number 2 because 2 is the largest integer less than or equal to the root R. |
||
=={{header|Go}}== |
|||
<lang go>package main |
|||
import "fmt" |
|||
func main() { |
|||
fmt.Println(root(3, 8)) |
|||
fmt.Println(root(3, 9)) |
|||
fmt.Println(root(2, 2e18)) |
|||
} |
|||
func root(N, X int) int { |
|||
for r := 1; ; { |
|||
x := X |
|||
for i := 1; i < N; i++ { |
|||
x /= r |
|||
} |
|||
x -= r |
|||
Δr := x / N |
|||
if x%N < 0 { |
|||
Δr-- |
|||
} |
|||
if Δr == 0 { |
|||
return r |
|||
} |
|||
r += Δr |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
2 |
|||
2 |
|||
1414213562 |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 23: | Line 58: | ||
7 <.@%: (2*10x^2*2000) |
7 <.@%: (2*10x^2*2000) |
||
29619362959451736245702628695019269518064618216015009169507699742781423769947484925822512257735101524178182602734424986961003971858127002794053824818478879396020132662403256874761276690431037137165264232256601651438511207764019815767975124455844526943932927494896013055497926678521360177960529077012650088983239249505488961115547364229473827474458408002500739618874659540108997885564940730803150961523774615079827002013042942440654069714159530336055547627964891459096727426898214883744931710925020592035759639587602673656267343846153343265577563529779031634608306646526796</lang> |
29619362959451736245702628695019269518064618216015009169507699742781423769947484925822512257735101524178182602734424986961003971858127002794053824818478879396020132662403256874761276690431037137165264232256601651438511207764019815767975124455844526943932927494896013055497926678521360177960529077012650088983239249505488961115547364229473827474458408002500739618874659540108997885564940730803150961523774615079827002013042942440654069714159530336055547627964891459096727426898214883744931710925020592035759639587602673656267343846153343265577563529779031634608306646526796</lang> |
||
=={{header|Python}}== |
=={{header|Python}}== |