-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kattis prob goldbach2 in python3 + notes
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Use the sieve of Eratosthenes to get all of the primes. Then initialise one pointer to the first prime, and the second pointer to the highest prime below the target number. Then iterate, with the following logic: | ||
|
||
- If the sum of these primes is too large, decrement the pointer to the higher prime. | ||
- If the sum of these primes is too small, incremenet the pointer to the lower prime. | ||
- If the sum is equal, add these primes to the answer set, and incrememnt the lower pointer. | ||
|
||
This runs in 0.1s in python3. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
def main(): | ||
is_prime = [True for _ in range(32005)] | ||
primes = [] | ||
for i in range(2, 32005): | ||
if is_prime[i]: | ||
primes.append(i) | ||
for j in range(i+i, 32005, i): | ||
is_prime[j] = False | ||
n = int(input()) | ||
for _ in range(n): | ||
m = int(input()) | ||
ans = [] | ||
i, j = 0, 0 | ||
while j < len(primes) and primes[j] < m: | ||
j += 1 | ||
while i <= j: | ||
if primes[i] + primes[j] == m: | ||
ans.append((primes[i], primes[j])) | ||
i += 1 | ||
elif primes[i] + primes[j] > m: | ||
j -= 1 | ||
else: | ||
i += 1 | ||
print('{} has {} representation(s)'.format(m, len(ans))) | ||
for a, b in ans: | ||
print('{}+{}'.format(a,b)) | ||
print() | ||
|
||
if __name__ == '__main__': | ||
main() |