Pell's equation: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: It's far more Pythonic to use multiple assignment than a helper method which uses arrays for pass-by-reference)
m (→‎{{header|REXX}}: added commas to the huge numbers, simplified the SAY expressions for the output of X and Y.)
Line 962: Line 962:


=={{header|REXX}}==
=={{header|REXX}}==
A little extra code was added to align and commatize the gihugeic numbers for readability.
<lang rexx>/*REXX program to solve Pell's equation for the smallest solution of positive integers. */
<lang rexx>/*REXX program to solve Pell's equation for the smallest solution of positive integers. */
numeric digits 2200 /*ensure enough decimal digs for answer*/
numeric digits 2200 /*ensure enough decimal digs for answer*/
parse arg $ /*obtain optional arguments from the CL*/
parse arg $ /*obtain optional arguments from the CL*/
if $=='' | $=="," then $= 61 109 181 277 /*Not specified? Then use the defaults*/
if $=='' | $=="," then $= 61 109 181 277 /*Not specified? Then use the defaults*/
d= 22 /*used for aligning the output numbers.*/
d= 28 /*used for aligning the output numbers.*/
do j=1 for words($); #= word($, j) /*process all the numbers in the list. */
do j=1 for words($); #= word($, j) /*process all the numbers in the list. */
parse value pells(#) with x y /*extract the two values of X and Y.*/
parse value pells(#) with x y /*extract the two values of X and Y.*/
say 'x^2 -'right(#,max(4,length(#))) "* y^2 == 1 when x="right(x, max(d,length(x))),
cx= comma(x); Lcx= length(cx); cy= comma(y); Lcy= length(cy); L#= length(#)
say 'x^2 -'right(#, max(4, L#)) "* y^2 == 1" ,
' and y='right(y, max(d,length(y)))
' when x='right(cx, max(d, Lcx)) " and y="right(cy, max(d, Lcy))
end /*j*/
end /*j*/
exit /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: parse arg ?; do jc=length(?)-3 to 1 by -3; ?= insert(',', ?, jc); end; return ?
floor: procedure; parse arg x; _= x % 1; return _ - (x < 0) * (x \= _)
floor: procedure; parse arg x; _= x % 1; return _ - (x < 0) * (x \= _)
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 993: Line 996:
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
x^2 - 61 * y^2 == 1 when x= 1766319049 and y= 226153980
x^2 - 61 * y^2 == 1 when x= 1,766,319,049 and y= 226,153,980
x^2 - 109 * y^2 == 1 when x= 158070671986249 and y= 15140424455100
x^2 - 109 * y^2 == 1 when x= 158,070,671,986,249 and y= 15,140,424,455,100
x^2 - 181 * y^2 == 1 when x= 2469645423824185801 and y= 183567298683461940
x^2 - 181 * y^2 == 1 when x= 2,469,645,423,824,185,801 and y= 183,567,298,683,461,940
x^2 - 277 * y^2 == 1 when x= 159150073798980475849 and y= 9562401173878027020
x^2 - 277 * y^2 == 1 when x= 159,150,073,798,980,475,849 and y= 9,562,401,173,878,027,020
</pre>
</pre>