Brilliant numbers: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Much quicker than before.) |
(Added Go) |
||
Line 29: | Line 29: | ||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|Go-rcu}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"math" |
|||
"rcu" |
|||
"sort" |
|||
) |
|||
var primes = rcu.Primes(1e8 - 1) |
|||
type res struct { |
|||
bc interface{} |
|||
next int |
|||
} |
|||
func getBrilliant(digits, limit int, countOnly bool) res { |
|||
var brilliant []int |
|||
count := 0 |
|||
pow := 1 |
|||
next := math.MaxInt |
|||
for k := 1; k <= digits; k++ { |
|||
var s []int |
|||
for _, p := range primes { |
|||
if p >= pow*10 { |
|||
break |
|||
} |
|||
if p > pow { |
|||
s = append(s, p) |
|||
} |
|||
} |
|||
for i := 0; i < len(s); i++ { |
|||
for j := i; j < len(s); j++ { |
|||
prod := s[i] * s[j] |
|||
if prod < limit { |
|||
if countOnly { |
|||
count++ |
|||
} else { |
|||
brilliant = append(brilliant, prod) |
|||
} |
|||
} else { |
|||
if next > prod { |
|||
next = prod |
|||
} |
|||
break |
|||
} |
|||
} |
|||
} |
|||
pow *= 10 |
|||
} |
|||
if countOnly { |
|||
return res{count, next} |
|||
} |
|||
return res{brilliant, next} |
|||
} |
|||
func main() { |
|||
fmt.Println("First 100 brilliant numbers:") |
|||
brilliant := getBrilliant(2, 10000, false).bc.([]int) |
|||
sort.Ints(brilliant) |
|||
brilliant = brilliant[0:100] |
|||
for i := 0; i < len(brilliant); i++ { |
|||
fmt.Printf("%4d ", brilliant[i]) |
|||
if (i+1)%10 == 0 { |
|||
fmt.Println() |
|||
} |
|||
} |
|||
fmt.Println() |
|||
for k := 1; k <= 13; k++ { |
|||
limit := int(math.Pow(10, float64(k))) |
|||
r := getBrilliant(k, limit, true) |
|||
total := r.bc.(int) |
|||
next := r.next |
|||
climit := rcu.Commatize(limit) |
|||
ctotal := rcu.Commatize(total + 1) |
|||
cnext := rcu.Commatize(next) |
|||
fmt.Printf("First >= %18s is %14s in the series: %18s\n", climit, ctotal, cnext) |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
First 100 brilliant numbers: |
|||
4 6 9 10 14 15 21 25 35 49 |
|||
121 143 169 187 209 221 247 253 289 299 |
|||
319 323 341 361 377 391 403 407 437 451 |
|||
473 481 493 517 527 529 533 551 559 583 |
|||
589 611 629 649 667 671 689 697 703 713 |
|||
731 737 767 779 781 793 799 803 817 841 |
|||
851 869 871 893 899 901 913 923 943 949 |
|||
961 979 989 1003 1007 1027 1037 1067 1073 1079 |
|||
1081 1121 1139 1147 1157 1159 1189 1207 1219 1241 |
|||
1247 1261 1271 1273 1333 1343 1349 1357 1363 1369 |
|||
First >= 10 is 4 in the series: 10 |
|||
First >= 100 is 11 in the series: 121 |
|||
First >= 1,000 is 74 in the series: 1,003 |
|||
First >= 10,000 is 242 in the series: 10,201 |
|||
First >= 100,000 is 2,505 in the series: 100,013 |
|||
First >= 1,000,000 is 10,538 in the series: 1,018,081 |
|||
First >= 10,000,000 is 124,364 in the series: 10,000,043 |
|||
First >= 100,000,000 is 573,929 in the series: 100,140,049 |
|||
First >= 1,000,000,000 is 7,407,841 in the series: 1,000,000,081 |
|||
First >= 10,000,000,000 is 35,547,995 in the series: 10,000,600,009 |
|||
First >= 100,000,000,000 is 491,316,167 in the series: 100,000,000,147 |
|||
First >= 1,000,000,000,000 is 2,409,600,866 in the series: 1,000,006,000,009 |
|||
First >= 10,000,000,000,000 is 34,896,253,010 in the series: 10,000,000,000,073 |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |