Sylvester's sequence: Difference between revisions
Content deleted Content added
Line 335: | Line 335: | ||
sylvester = iterate (\x -> x * (x-1) + 1) 2</lang> |
sylvester = iterate (\x -> x * (x-1) + 1) 2</lang> |
||
=={{header|jq}}== |
|||
<lang jq># Generate the sylvester integers: |
|||
def sylvester: |
|||
foreach range(0; infinite) as $i ({prev: 1, product: 1}; |
|||
.product *= .prev |
|||
| .prev = .product + 1; |
|||
.prev);</lang> |
|||
Left padding: |
|||
<lang jq> |
|||
def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .; |
|||
def lpad($len): lpad($len; " "); |
|||
def lpad: lpad(4);</lang> |
|||
The task: |
|||
<lang jq>[limit(10; sylvester)] |
|||
| "First 10 Sylvester numbers:", |
|||
(range(0;10) as $i | "\($i+1|lpad) => \(.[$i])"), |
|||
"", |
|||
"Sum of reciprocals of first 10 is approximately: \(map( 1/ .) | add)" |
|||
</lang> |
|||
{{out}} |
|||
For integer precision, we will use `gojq`, the "go" implementation of jq. |
|||
<pre>First 10 Sylvester numbers: |
|||
1 => 2 |
|||
2 => 3 |
|||
3 => 7 |
|||
4 => 43 |
|||
5 => 1807 |
|||
6 => 3263443 |
|||
7 => 10650056950807 |
|||
8 => 113423713055421844361000443 |
|||
9 => 12864938683278671740537145998360961546653259485195807 |
|||
10 => 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443 |
|||
Sum of reciprocals of first 10 is approximately: 0.9999999999999999</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<lang julia>sylvester(n) = (n == 1) ? big"2" : prod(sylvester, 1:n-1) + big"1" |
<lang julia>sylvester(n) = (n == 1) ? big"2" : prod(sylvester, 1:n-1) + big"1" |
||