Jump to content

Euclid's algorithm

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
tim1234

tim1234

    Newbie

  • Members
  • Pip
  • 4 posts
I've written a program in C to find the highest common factor of two integers, i need to add to it so that I can solve the ax + by = hcf(a,b) where x and y are integers. I've managed to program a trial and error method but that takes ages. Any ideas?

Cheers

#2
tim1234

tim1234

    Newbie

  • Members
  • Pip
  • 4 posts
don't worry i worked it out :)

#3
Cosmet

Cosmet

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
What was the solution?

#4
tim1234

tim1234

    Newbie

  • Members
  • Pip
  • 4 posts
i needed to perform a series of recursive relationships which i did using arrays.

let a[0] = m, a[1] = n
q[k] = a[k-1]/a[k]
a[k+1] = a[k-1] - a[k]q[k]
if a[p+1]=0 then a[p] is hcf m,n

let x[0]=1, x[1]=0, y[0]=0 y[1]=1 and use the same recursion as above and you get a[p] = nx[p] + my[p]

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I posted some code for this in another post...Moving Elements in an Array.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog