Public Group

#### Archived

This topic is now archived and is closed to further replies.

# i

This topic is 6177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i [Edited by - aeleneski on April 24, 2006 11:23:05 PM]

##### Share on other sites
This function would probably be extremely slow but heres one solution I think

int numerator, denomenator; // set these to something
int smallest; // holds the smaller of the two variables
int gcf; // greatest common factor

// determine which number is smallest, doesn''t matter if equal
if ( numerator >= denomenator )
{ smallest = numerator; }
else
{ smallest = denomenator; }

for ( int i = smallest; i > 0; i++ )
{
// if both numbers divide evenly, then it is divisible by i
if ( ( numerator % i == 0 ) && ( denomenator % i == 0 ))
{ gcf = i; }
}

I''m still learning C++ so if there are any more complex functions to do this I am unaware of them.

##### Share on other sites
Here''s some source code I made a while ago:
 ` // Utilitiesvoid rational::simplify() // Simplify rational{ double x=gcd(num, denom); num/=x; denom/=x;}long rational::gcd(long x, long y) // Find greatest common divisor{ x=labs(x); // long absolute value y=labs(y); while(x != y) { if(x

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633319
• Total Posts
3011347
• ### Who's Online (See full list)

There are no registered users currently online

×