Ruby | math design?

Situation:

  • I am writing a program for solving prime numbers. I need to solve problems accurate to 4x ^ 2 + y ^ 2 = n, where n is a known variable.
  • Yes, it must be Ruby.
  • I like to spend a lot of time in this project.
  • I would like, preferably, to write the solution algorithm itself for the equations and include it as part of this project.

What I would really love:

  • If someone can provide me with a link to a guide, a website or values ​​related to the construction of formal algorithms specifically related to solving algebraic equations, or provide me with information that you think will help you in my searches.
  • Please do not suggest using a different language. I would also be grateful if, prior to the reply, you agree that I really really want to do this. There are no time or time limits in this project, and this is not for profit. This is for my own education.

Note:

  • I am not directly opposed to implementing and using an existing math library / module / something for Ruby, but for me it is preferable.

Closing comments:

The problem is that I know how to solve these equations manually / using a calculator, but I'm not sure how to solve them in the code.

+6
source share
2 answers

It seems to you that you are trying to implement Atkin's Sieve , then you probably also know that 4x ^ 2 + y ^ 2 = n is only the first of three equations. I do not want to spoil your pleasure, and thus, only that one realizes it below. If you are stuck, just comment on this answer and I will get back to you.

max = 100 primes = Array.new(max + 1) { false } sqrt = Math.sqrt(max) 1.upto(sqrt) do |x| 1.upto(sqrt) do |y| n = 4 * x**2 + y**2 primes[n] ^= true if n <= max && (n % 12 == 1 || n % 12 == 5) end end 
+2
source

I suppose you are implementing the Sieve of Atkin. In this case, you are not actually solving the equation. Look at the original paper for the actual algorithm.

+2
source

Source: https://habr.com/ru/post/915173/


All Articles