The MASM Forum

Miscellaneous => The Orphanage => Topic started by: Gunther on December 29, 2023, 09:31:37 AM

Title: Diophantine linear equation system
Post by: Gunther on December 29, 2023, 09:31:37 AM
Here's a little puzzle that fits well between the years 2023 and 2024. A few years ago, this was a task
at the International Mathematical Olympiad. But don't worry; the problem is really not difficult to solve.
I've modified it slightly. Here's the exercise. The following system of linear equations is to be solved:
x, y, z should be natural numbers and >= 1.

I posted it here in the Soap Box because there is no place for it in other subforums. I wish the
interested members a lot of fun with the solution.
Title: Re: Diophantine linear equation system
Post by: raymond on December 29, 2023, 01:03:02 PM
 :thumbsup:  :thumbsup: 
Interesting "little" algebra problem. Reminds me a bit of high school.  :eusa_clap:
Title: Re: Diophantine linear equation system
Post by: Gunther on December 29, 2023, 08:57:21 PM
Raymond,

Quote from: raymond on December 29, 2023, 01:03:02 PMInteresting "little" algebra problem. Reminds me a bit of high school.  :eusa_clap:

Yes, indeed. Unfortunately we have 3 variables and only 2 equations; but there is additional information
about x, y, z. The standard methods (LU decomposition, Wilkinson orthogonalization, etc.) wo'nt work
here. You have to be creative. But once the right idea has been found, the solution is very simple.

I don't think it will be very long before the first correct solution is posted here. So let's wait a
little while longer.
Title: Re: Diophantine linear equation system
Post by: jack on December 29, 2023, 09:30:48 PM
"Extended Euclidean Algorithm" ?
Title: Re: Diophantine linear equation system
Post by: Gunther on December 29, 2023, 10:35:55 PM
Jack,

thank you for your answer.

Quote from: jack on December 29, 2023, 09:30:48 PM"Extended Euclidean Algorithm" ?
You can try that. But I don't think it will work. Incidentally, there is really only one solution.
Title: Re: Diophantine linear equation system
Post by: HSE on December 30, 2023, 04:53:38 AM
Hi Gunther,

I failed to cheat  :biggrin:

Genetic Algorithm is trapped in a local solution:

x 22,2193052890
y 90,0463773843 ec1 = 2023,998484
z 22,2305353835 ec2 = 2022,998485

interaccions = 12000000000;

But very nice to play with this 10 years old programs :thumbsup:
(specially because I will need to use them very soon  :biggrin: )

Regards, HSE

Title: Re: Diophantine linear equation system
Post by: Gunther on December 30, 2023, 05:48:22 AM
HSE,

nice try, but wrong result.

As I have already written: You have to be creative.
Title: Re: Diophantine linear equation system
Post by: jack on December 30, 2023, 07:49:11 AM
I solved it by cheating  :badgrin:
Title: Re: Diophantine linear equation system
Post by: Gunther on December 30, 2023, 07:57:17 AM
Jack,

Quote from: jack on December 30, 2023, 07:49:11 AMI solved it by cheating  :badgrin:

why not? What's your cheating result?
Title: Re: Diophantine linear equation system
Post by: jack on December 30, 2023, 08:01:53 AM
x=674, y=2, z=675 my cheat https://youtu.be/fd12cV8ZFvA?si=jkIJjj-IvJvrBtyH (https://youtu.be/fd12cV8ZFvA?si=jkIJjj-IvJvrBtyH)
Title: Re: Diophantine linear equation system
Post by: Gunther on December 30, 2023, 08:06:17 AM
Okay. Check your PM.
Title: Re: Diophantine linear equation system
Post by: raymond on January 03, 2024, 06:00:37 AM
My son was over for dinner with us yesterday. Lately, he has been playing with AI to create web pages for his employer. We couldn't resist feeding it this problem which only requires basic high school algebra and a half-ounce of analytical logic.

As I expected, it did have a basic knowledge of algebra but was totally lacking the required logic. After subtracting one equation from the other, it simply tried  a "brute force" approach starting with x=1 and then iterating the y and z values to fit the equations. :eusa_boohoo:

Obviously, it NEVER got to a solution. So much for AI. :joking:  :joking:  :joking:
Title: Re: Diophantine linear equation system
Post by: HSE on January 03, 2024, 09:55:54 AM
Hi Raymond,

Quote from: raymond on January 03, 2024, 06:00:37 AMObviously, it NEVER got to a solution. So much for AI.

It's a good trick :thumbsup: 

The shame is AI don't know assembly  :biggrin:

diophantine proc
    local x_val:qword, y_val:qword, z_val:qword

    mov r8,  2024
    @@x:
        mov r9,  2024
        @@y:
            mov r10, 2024
            @@z: 

                mov rax, r9
                mov rcx, r10
                mul rcx
                add rax, r8
                cmp rax, 2024
                jne noes

                mov rax, r8
                mov rcx, r9
                mul rcx
                add rax, r10
                cmp rax, 2023
                je solution

          noes:

            dec r10
            jnz @@z
        dec r9
        jnz @@y
    dec r8
    jnz @@x
    conout "This fail!",lf,lf
    ret

  solution:
    mov x_val, r8   
    mov y_val, r9   
    mov z_val, r10
   
    conout " x = ", str$(x_val),lf
    conout " y = ", str$(y_val),lf
    conout " z = ", str$(z_val),lf,lf

    ret
diophantine endp

But process take almost 3 seconds here  :undecided:
Title: Re: Diophantine linear equation system
Post by: NoCforMe on January 03, 2024, 10:40:03 AM
Quote from: HSE on January 03, 2024, 09:55:54 AM
Quote from: raymond on January 03, 2024, 06:00:37 AMObviously, it NEVER got to a solution. So much for AI.
It's a good trick :thumbsup: 
The shame is AI don't know assembly  :biggrin:
I know you were kinda sorta joking, but really, what possible difference could that make, whether AI "knows" assembly? The language used to code a solution makes no difference; they could use COBOL for all that matters. The point is it hasn't a clue about how to go about solving this problem.

Like the man said, so much for AI. (Artificial ignorance in my book.) Given all the real problems needing solutions in the world, a complete waste of time.

BTW, looking at your code, I have no clue how that works. Do you mind explaining it?
Title: Re: Diophantine linear equation system
Post by: HSE on January 03, 2024, 10:51:08 AM
Hi NoC,

Quote from: NoCforMe on January 03, 2024, 10:40:03 AMBTW, looking at your code, I have no clue how that works. Do you mind explaining it?

Code have 3 loops exploring values between 2024 and 1 for each variable. In inner loop results of equations are evaluated. Just that.

No macros, you can see  :biggrin:

Title: Re: Diophantine linear equation system
Post by: NoCforMe on January 03, 2024, 11:02:17 AM
So, it's basically a brute force technique.
How about applying some of the logic that was shown in that YouTube video that Jack posted (https://masm32.com/board/index.php?msg=126036) here? Is that possible?
Title: Re: Diophantine linear equation system
Post by: HSE on January 03, 2024, 11:21:20 AM
Quote from: NoCforMe on January 03, 2024, 11:02:17 AMSo, it's basically a brute force technique.
How about applying some of the logic that was shown in that YouTube video that Jack posted (https://masm32.com/board/index.php?msg=126036) here? Is that possible?


Yes. AI solution  :biggrin:

No video... yet  :rolleyes:
Title: Re: Diophantine linear equation system
Post by: raymond on January 03, 2024, 03:11:00 PM
Here was my approach to solve it within minutes, using logic which AI does not have.

1st, as did AI. was to subtract the two equations.

1. x  + yz = 2024
2. xy + z  = 2023
______________
x + yz -xy - z = 1

2nd, group some unknowns together as follows (where AI is lacking):

(yz-z) - (xy-x) = 1
z(y-1) - x(y-1) = 1
(z-x)(y-1) = 1

Then it's a simple matter to observe that both components of the multiplication must be integral values based on the problem definition, AND, the only way for the equation to be true is if both are being equal to 1 (AI does not yet seem able to make such observations and deductions).
Therefore, y = 2
and, z = x+1

Replacing those values in the second equation (xy+z=2023),
2x + (x+1) = 3x+1 = 2023
             3x = 2023-1 = 2022
              x = 2022/3 = 674
 and then     z = x+1    = 675
 :greenclp:


Title: Re: Diophantine linear equation system
Post by: HSE on January 03, 2024, 03:22:22 PM
Hi!

I can sleep, but I have another solution:

  ay + (1 + a) = 2023

Anyway some brute force, testing first 3 integer results for y sequence :biggrin: (is the second)

Loops solution take me 10 minutes, this hours. Perhaps, Raymond solution could take me weeks  :biggrin:

Fantastic  :thumbsup:  (I hope to sleep now)

Later: Mmm, not so clear.

I solve " a = (2023-1)/(1+y) " for " y = 1, 2, 3, 4, etc "  :thumbsup:
Title: Re: Diophantine linear equation system
Post by: Gunther on January 04, 2024, 07:10:12 AM
Raymond,

Quote from: raymond on January 03, 2024, 03:11:00 PMHere was my approach to solve it within minutes, using logic which AI does not have.

precisely. These are the right steps.
None of this is witchcraft.
Title: Re: Diophantine linear equation system
Post by: daydreamer on January 06, 2024, 05:54:15 PM
At uni we learned matrices xyzw way of solve things,is that  possible Gunther?
Fastest way for humans to calculate things is autistic math talent,like in that Tom Cruise movie
@raymond
Now when you posted solution on the net you might have taught AI to solve it  :biggrin: