Primes with digits in nondecreasing order: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add J) |
(Added Go) |
||
Line 411: | Line 411: | ||
next i : print</lang> |
next i : print</lang> |
||
{{out}}<pre>2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677</pre> |
{{out}}<pre>2 3 5 7 11 13 17 19 23 29 37 47 59 67 79 89 113 127 137 139 149 157 167 179 199 223 227 229 233 239 257 269 277 337 347 349 359 367 379 389 449 457 467 479 499 557 569 577 599 677</pre> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|Go-rcu}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"rcu" |
|||
) |
|||
func nonDescending(p int) bool { |
|||
var digits []int |
|||
for p > 0 { |
|||
digits = append(digits, p%10) |
|||
p = p / 10 |
|||
} |
|||
for i := 0; i < len(digits)-1; i++ { |
|||
if digits[i+1] > digits[i] { |
|||
return false |
|||
} |
|||
} |
|||
return true |
|||
} |
|||
func main() { |
|||
primes := rcu.Primes(999) |
|||
var nonDesc []int |
|||
for _, p := range primes { |
|||
if nonDescending(p) { |
|||
nonDesc = append(nonDesc, p) |
|||
} |
|||
} |
|||
fmt.Println("Primes below 1,000 with digits in non-decreasing order:") |
|||
for i, n := range nonDesc { |
|||
fmt.Printf("%3d ", n) |
|||
if (i+1)%10 == 0 { |
|||
fmt.Println() |
|||
} |
|||
} |
|||
fmt.Printf("\n%d such primes found.\n", len(nonDesc)) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Primes below 1,000 with digits in non-decreasing order: |
|||
2 3 5 7 11 13 17 19 23 29 |
|||
37 47 59 67 79 89 113 127 137 139 |
|||
149 157 167 179 199 223 227 229 233 239 |
|||
257 269 277 337 347 349 359 367 379 389 |
|||
449 457 467 479 499 557 569 577 599 677 |
|||
50 such primes found. |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |