Find squares n where n+1 is prime: Difference between revisions
Content added Content deleted
(Created page with "{{Draft task}} ;Task: <br> Find squares '''n''' where '''n+1''' is prime and '''n<1.000''' =={{header|Ring}}== <lang ring> load "stdlib.ring" row = 0 limit = 1000 see "workin...") |
(Added Wren) |
||
Line 44: | Line 44: | ||
Found 10 numbers |
Found 10 numbers |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-math}} |
|||
<lang ecmascript>import "./math" for Int |
|||
var squares = [] |
|||
for (i in 1..1000.sqrt.floor) { |
|||
var n = i * i |
|||
if (Int.isPrime(n+1)) squares.add(n) |
|||
} |
|||
System.print("There are %(squares.count) square numbers 'n' where 'n+1' is prime, viz:") |
|||
System.print(squares)</lang> |
|||
{{out}} |
|||
<pre> |
|||
There are 10 square numbers 'n' where 'n+1' is prime, viz: |
|||
[1, 4, 16, 36, 100, 196, 256, 400, 576, 676] |
|||
</pre> |
</pre> |
Revision as of 15:17, 16 December 2021
Find squares n where n+1 is prime is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Find squares n where n+1 is prime and n<1.000
Ring
<lang ring> load "stdlib.ring" row = 0 limit = 1000 see "working..." + nl
for n = 1 to limit-1
if issquare(n) and isprime(n+1) row++ see "" + n +nl ok
next
see "Found " + row + " numbers" + nl see "done..." + nl
func issquare(x)
for n = 1 to sqrt(x) if x = pow(n,2) return 1 ok next return 0
</lang>
- Output:
working... 1 4 16 36 100 196 256 400 576 676 Found 10 numbers done...
Wren
<lang ecmascript>import "./math" for Int
var squares = [] for (i in 1..1000.sqrt.floor) {
var n = i * i if (Int.isPrime(n+1)) squares.add(n)
} System.print("There are %(squares.count) square numbers 'n' where 'n+1' is prime, viz:") System.print(squares)</lang>
- Output:
There are 10 square numbers 'n' where 'n+1' is prime, viz: [1, 4, 16, 36, 100, 196, 256, 400, 576, 676]