CalmoSoft primes: Difference between revisions
Content added Content deleted
(Removed Algol 68 stretch - it got the answer wrong ... : {) |
(→{{header|Go}}: Updated in line with Wren entry of which it is a translation.) |
||
Line 273: | Line 273: | ||
"fmt" |
"fmt" |
||
"rcu" |
"rcu" |
||
" |
"time" |
||
) |
) |
||
var start = time.Now() |
|||
⚫ | |||
⚫ | |||
const max = 50_000_000 |
|||
⚫ | |||
⚫ | |||
func calmoPrimes(limit int) (int, []int, []int, []int) { |
|||
var pc, sum int |
|||
if limit < max { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
break |
|||
⚫ | |||
⚫ | |||
} else { |
|||
⚫ | |||
} |
|||
for i := 0; i < pc; i++ { |
|||
⚫ | |||
} |
|||
longest := 0 |
longest := 0 |
||
var sIndices, eIndices []int |
var sIndices, eIndices, sums []int |
||
for i := 0; i < pc; i++ { |
for i := 0; i < pc; i++ { |
||
⚫ | |||
break |
|||
} |
|||
if i > 0 { |
|||
⚫ | |||
} |
|||
sum2 := sum |
|||
for j := pc - 1; j >= i; j-- { |
for j := pc - 1; j >= i; j-- { |
||
temp := j - i + 1 |
temp := j - i + 1 |
||
Line 287: | Line 312: | ||
break |
break |
||
} |
} |
||
if j < pc-1 { |
|||
sum2 -= primes[j+1] |
|||
} |
|||
if rcu.IsPrime(sum2) { |
|||
if temp > longest { |
if temp > longest { |
||
longest = temp |
longest = temp |
||
sIndices = []int{i} |
sIndices = []int{i} |
||
eIndices = []int{j} |
eIndices = []int{j} |
||
sums = []int{sum2} |
|||
} else { |
} else { |
||
sIndices = append(sIndices, i) |
sIndices = append(sIndices, i) |
||
eIndices = append(eIndices, j) |
eIndices = append(eIndices, j) |
||
sums = append(sums, sum2) |
|||
} |
} |
||
break |
break |
||
Line 301: | Line 330: | ||
} |
} |
||
} |
} |
||
return longest, sIndices, eIndices, sums |
|||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
for _, limit := range []int{100, 250, 5000, 10000, 500000, 50000000} { |
|||
⚫ | |||
longest, sIndices, eIndices, sums := calmoPrimes(limit) |
|||
fmt.Println("For primes up to", rcu.Commatize(limit), "the longest sequence(s) of CalmoSoft primes") |
|||
⚫ | |||
⚫ | |||
⚫ | |||
for i := 0; i < len(sIndices); i++ { |
|||
⚫ | |||
cp2 := primes[eIndices[i]-5 : eIndices[i]+1] |
|||
cps := "" |
|||
for _, p := range cp1 { |
|||
cps += fmt.Sprintf("%d + ", p) |
|||
} |
} |
||
cps += ".. + " |
|||
for _, p := range cp2 { |
|||
cps += fmt.Sprintf("%d + ", p) |
|||
} |
|||
fmt.Printf("%s = %s\n", cps[:len(cps)-3], rcu.Commatize(sums[i])) |
|||
} |
} |
||
⚫ | |||
cps += " = " + strconv.Itoa(sum) + " which is prime" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
fmt.Printf("Took %d ms\n", time.Since(start).Milliseconds()) |
|||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
For primes up to 100 the longest sequence(s) of CalmoSoft primes |
|||
having a length of 21 is/are: |
|||
⚫ | |||
For primes up to 250 the longest sequence(s) of CalmoSoft primes |
|||
having a length of 49 is/are: |
|||
11 + 13 + 17 + 19 + 23 + 29 + .. + 223 + 227 + 229 + 233 + 239 + 241 = 5,813 |
|||
For primes up to 5,000 the longest sequence(s) of CalmoSoft primes |
|||
having a length of 665 is/are: |
|||
7 + 11 + 13 + 17 + 19 + 23 + .. + 4957 + 4967 + 4969 + 4973 + 4987 + 4993 = 1,543,127 |
|||
For primes up to 10,000 the longest sequence(s) of CalmoSoft primes |
|||
having a length of 1,223 is/are: |
|||
3 + 5 + 7 + 11 + 13 + 17 + .. + 9883 + 9887 + 9901 + 9907 + 9923 + 9929 = 5,686,633 |
|||
7 + 11 + 13 + 17 + 19 + 23 + .. + 9901 + 9907 + 9923 + 9929 + 9931 + 9941 = 5,706,497 |
|||
For primes up to 500,000 the longest sequence(s) of CalmoSoft primes |
|||
having a length of 41,530 is/are: |
|||
2 + 3 + 5 + 7 + 11 + 13 + .. + 499787 + 499801 + 499819 + 499853 + 499879 + 499883 = 9,910,236,647 |
|||
For primes up to 50,000,000 the longest sequence(s) of CalmoSoft primes |
|||
having a length of 3,001,117 is/are: |
|||
7 + 11 + 13 + 17 + 19 + 23 + .. + 49999699 + 49999711 + 49999739 + 49999751 + 49999753 + 49999757 = 72,618,848,632,313 |
|||
Took 270 ms |
|||
⚫ | |||
</pre> |
</pre> |
||