Legendre prime counting function: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Impoved 'memoization' version.) |
(→{{header|Go}}: Improved 'memoization' version.) |
||
Line 231: | Line 231: | ||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
===Memoized=== |
===Memoized=== |
||
This runs in about |
This runs in about 1.9 seconds which is surprisingly slow for Go. However, Go has the same problem as Wren in not being able to use lists for map keys. I tried using a 2-element array for the key but this was a second slower than the Cantor function. |
||
The alternative of sieving a billion numbers and then counting them takes about 4.8 seconds using the present library function though this could be cut to 1.2 seconds using a third party library such as primegen. |
The alternative of sieving a billion numbers and then counting them takes about 4.8 seconds using the present library function though this could be cut to 1.2 seconds using a third party library such as primegen. |
||
Line 242: | Line 242: | ||
"rcu" |
"rcu" |
||
) |
) |
||
var limit = int(math.Sqrt(1e9)) |
|||
⚫ | |||
⚫ | |||
func cantorPair(x, y int) int { |
func cantorPair(x, y int) int { |
||
Line 252: | Line 248: | ||
} |
} |
||
return (x*x + 3*x + 2*x*y + y + y*y) / 2 |
return (x*x + 3*x + 2*x*y + y + y*y) / 2 |
||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
Line 271: | Line 254: | ||
return 0 |
return 0 |
||
} |
} |
||
⚫ | |||
a := pi(int(math.Sqrt(float64(n)))) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
a := len(primes) |
|||
⚫ | |||
var phi func(x, a int) int // recursive closure |
|||
⚫ | |||
if a < 1 { |
|||
return x |
|||
} |
|||
if a == 1 { |
|||
return x - (x >> 1) |
|||
} |
|||
⚫ | |||
if x <= pa { |
|||
return 1 |
|||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return phi(n, a) + a - 1 |
return phi(n, a) + a - 1 |
||
} |
} |