How to break integers with mysql

When executing my project, I need to find a simple integer factorization using mysql, which, in my opinion, is an efficient way to query along with doing the whole recursive thing.

and I want to achieve to find the prime numbers that make up an integer.

Example

: for 102 factorial numbers: 17, 3, 2

Thanks.

+4
source share
1 answer

back of envelope strategy (the programmed loop for step 2 is still required)

  • create a table of "prime numbers" with one int column (primary key)

  • run this loop:

    for $x = 2 to $n { execute(" insert into primes (id) select $x where not exists (select * from primes as p where p.id <= sqrt($x) AND ($x mod p.id) > 0)") } 
  • use the subquery above to list the results for a specific $ x

this solution will work for values ​​up to $ n ^ 2. Step 2 can only be improved by testing numbers over 9 with the last digits 1,3,7,9.

0
source

All Articles