Solver Find Greatest Common Factor
Algebra
->
Divisibility and Prime Numbers
-> Solver Find Greatest Common Factor
Log On
Algebra: Divisibility and Prime Numbers
Section
Solvers
Solvers
Lessons
Lessons
Answers archive
Answers
Source code of 'Find Greatest Common Factor'
This Solver (Find Greatest Common Factor)
was created by by
ichudov(507)
:
View Source
,
Show
,
Put on YOUR site
About ichudov
:
I am not a paid tutor, I am the owner of this web site!
==section input Find Greatest Common Factor (GCF) of *[input a=24] and *[input b=64] ==section solution perl print "First, factorise $a and $b: "; my $fa = solve( "factor_any_number", $a ); my $fb = solve( "factor_any_number", $b ); print " Factors of $a: $fa Factors of $b: $fb.\n\n"; my @fa = split( /,/, $fa ); my @fa1 = @fa; my @fb = split( /,/, $fb ); my @fb1 = @fb; my @fa2 = (); my @fb2 = (); my $fbcommon = (); foreach my $f (@fa) { # check if it is in b my $found = 0; for( my $i = 0; !$found && $i <= $#fb1; $i++ ) { if( $f == $fb1[$i] ) { my $r = "<FONT COLOR=RED>$f</FONT>"; push @fa2, $r; push @fb2, $r; $found = 1; splice( @fb1, $i, 1 ); #print "\n\n<I>$r: fb1=" . join( "*", @fb1 ). "</I>\n\m"; push @fbcommon, $f; last; } } unless( $found ) { push @fa2, $f; } } push @fb2, @fb1; print "Highlight common factors: $a: " . join( ",", @fa2 ) . " $b: " . join( ",", @fb2 ) . " Common factors: " . join( ",", @fbcommon ) . "\n\n"; my $product = 1; foreach my $f (@fbcommon) { $product *= $f; } if( $#fbcommon >= 0 ) { print "The product of these common factors is:" . join( '*', @fbcommon ) . "=$product.\n\nThis number, $product, is your greatest common factor: <U>Greatest Common Factor( $a, $b ) = $product</U>. "; } else { print "There are no common divisors! So, the greatest common factor is 1."; } $gcf = $product; ==section output gcf ==section check a=24 b=67 gcf=1 a=24 b=60 gcf=12 a=24 b=64 gcf=8