Hello monks (meant here in a unisex context)
I have several problem sets wherein I am needing to find the GCD (Greatest Common Divisor) between 2 large numbers. Now I wrote a program that took in the 2 numbers and printed out all the divisors of those numbers. The lists were very long however; so I thought to myself; self, you should make the program compare the 2 lists for the greatest common one, or at least for all the common ones. In this though I failed. The following is my code, and I was hoping one of the geniuses who oft roam the monestery could quickly see my problem and offer some suggestion. I am sure it is a very stupid mistake.
#!/usr/bin/perl
use strict;
my @thing;
my @thing1;
my $test=0;
my ($i,$j,$k,$arg1,$arg2);
while(my $argv=shift){
if($test==0){
$arg1=$argv;
}
if($test>0){
$arg2=$argv;
}
for($i=1;$i<=$argv;$i++){
if(!($argv%$i)){
if($test==0){
@thing[$i]=$i;
}
if($test>0){
@thing1[$i]=$i;
}
}
}
$test++;
}
for($j=0;$j<=$arg1;$j++){
for($k=0;$k<=$arg2;$k++){
if(@thing[$j] == @thing1[$k]){
print "A common divisor of $arg1 and $arg2 is: ". @thing[$j] . "\
+n";
}
}
}
~heise2k~
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.