Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
0285 - X2Y2K
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Rezolvare == <syntaxhighlight lang="python" line> #0285 import math def is_sum_of_squares(x, y, k): """ Returns True if x^2 + y^2 is equal to k, False otherwise. """ return x**2 + y**2 == k def has_valid_factors(k): """ Returns True if k has only factors of the form 4n+1, False otherwise. """ for i in range(2, int(math.sqrt(k))+1): if k % i == 0: p = 0 while k % i == 0: k //= i p += 1 if i % 4 == 3 and p % 2 != 0: return False if k % 4 == 3: return False return True def find_sum_of_squares_pairs(k): """ Finds all pairs of natural numbers (x,y) such that x^2 + y^2 is equal to k. Returns a list of tuples. """ pairs = [] for x in range(1, int(math.sqrt(k))+1): for y in range(x, int(math.sqrt(k))+1): if is_sum_of_squares(x, y, k) and has_valid_factors(k): pairs.append((x, y)) return pairs if __name__ == '__main__': k = int(input("Introduceti valoarea lui k: ")) if 2 <= k <= 1000000000: print("Datele sunt introduse corect.") pairs = find_sum_of_squares_pairs(k) if pairs: for pair in pairs: print(pair[0], pair[1]) else: print("Nu exista perechi (x,y) cu x, y > 0 astfel incat x^2 + y^2 = k.") else: print("Datele nu corespund restricțiilor impuse.") </syntaxhighlight> '''Explicatie cod:''' Acest cod contine trei functii care lucreaza impreuna pentru a gasi toate perechile de numere naturale (x,y) astfel incat x^2 + y^2 sa fie egal cu k. Mai exact: is_sum_of_squares(x, y, k) verifica daca x^2 + y^2 este egal cu k si returneaza True sau False in consecinta. has_valid_factors(k) verifica daca toate factorii lui k sunt de forma 4n+1 si returneaza True sau False in consecinta. find_sum_of_squares_pairs(k) parcurge toate perechile de numere naturale (x,y) cu x <= y si verifica daca x^2 + y^2 este egal cu k si daca k are doar factori de forma 4n+1. Daca conditiile sunt satisfacute, perechea este adaugata la o lista si, la sfarsit, lista este returnata. La final, codul verifica daca valoarea k introdusa de utilizator este intre 2 si 1000000000, inclusiv. Daca nu este, se afiseaza un mesaj de eroare. Daca este, se cauta toate perechile (x,y) astfel incat x^2 + y^2 = k si se afiseaza rezultatul, sau un mesaj care indica ca nu exista astfel de perechi. De asemenea, toate acestea se intampla doar daca acest script este rulat direct (if name == 'main':), nu daca este importat ca modul in alt script.
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width