Victory Road Archive

You are viewing an archive of Victory Road.

Victory Road closed on January 8, 2018. Thank you for making us a part of your lives since 2006! Please read this thread for details if you missed it.

Technology → Perl Used for Math Homework

Page 1 of 1

1. FreezeWarp said on January 30, 2010, 06:23:54 AM (-08:00)

Kyurem
2,186 posts

I wrote this as a solution to my math homework; the problem was figuring out what the greatest number was that could not be made by adding 6, 9, and 20.

Code:
#!/usr/bin/perl
# (c) J Parsons
# V.02.2 (1/6/09)
# math.pl [int1] [int2] [int3] [length]

sub check {
  # $x is the value to be tested, $int1 is the first integer to test for, and so on.
  my($x,$int1,$int2,$int3) = @_;
  # Copy $x into $y.
  $y = $x;
  for ($z = $y; $z >= 0; $z -= $int1) {
    $v1 += 1;
  }
  for ($v1; $v1 >= 0; $v1 -= 1) {
    $y -= ($v1 * $int1);
    $a = $y;
    if ($y == 0) {
      return "$int1 × $v1  $int2 × 0  $int3 × 0";
    }
    if ($int2) {
      for ($z = $y; $z >= 0; $z -= $int2) {
        $v2 += 1;
      }
      for ($v2; $v2 >= 0; $v2 -= 1) {
        $y -= ($v2 * $int2);
        if ($y == 0) {
          return "$int1 × $v1  $int2 × $v2  $int3 × 0";
        }
	if ($int3 > 1) {
          for ($z = $y; $z >= 0; $z -= $int3) {
            $v3 += 1;
          }
          for ($v3; $v3 >= 0; $v3 -= 1) {
            $y -= ($v3 * $int3);
            if ($y == 0) {
              return "$int1 × $v1  $int2 × $v2  $int3 × $v3";
            }
          }
        }
        $y = $a;
      }
    }
    $y = $x;
  }
}

if (!$ARGV[0]) {
  die ("math.pl [Integer 1] [Integer 2 = false] [Integeer 3 = false] [Length = 100] [Number = false]\nInteger 1 - The first integer that will be checked for.\nInteger 2 - The second integer that will be checked for.\nInteger 3 - The third integer that will be checked for.\nLength - The duration numbers will be checked for.\nNumber - Overrides length by checking for a specific number.");
}

if (!$ARGV[4]) {
  for ($b = 0; $b <= (($ARGV[3]) ? $ARGV[3] : 100); $b += 1) {
    if ($combo = check($b,$ARGV[0],$ARGV[1],$ARGV[2])) {
      print "$b - true    $combo\n";
    }
    else {
      print "$b - false\n";
    }
  }
}
else {
  if ($combo = check($ARGV[4],$ARGV[0],$ARGV[1],$ARGV[2])) {
     print "$ARGV[4] - true    $combo\n";
  }
  else {
    print "$ARGV[4] - false\n";
  }
}
The answer is 43, by the way.

Likes 1 – KingOfKYA
Page 1 of 1

User List - Contact - Privacy Statement - Lycanroc.Net