Created
October 6, 2015 21:12
-
-
Save carlosonunez/ae5d52f3aaf1d19f7faf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# URI: https://www.reddit.com/r/dailyprogrammer/comments/3nkanm/20151005_challenge_235_easy_ruthaaron_pairs/ | |
# Ruth-Aaron Pairs. | |
function GetRuthAaronPairs { | |
# GetSumOfPrimeFactors: Calculates and sums the prime factors for a given number. | |
function GetSumOfPrimeFactors { | |
param ([int]$Number) | |
$sum = 0 | |
$current = $Number | |
for ( $i = 2; $current -gt 1 ; $i++ ) { | |
if ( $current % $i -eq 0 ) { | |
$sum += $i | |
while ( $current % $i -eq 0 ) { | |
$current = $current/$i | |
} | |
} | |
} | |
return $sum | |
} | |
$NumberOfPairs = [int](Read-Host "Number of pairs") | |
$pairs = @() | |
for ($i=0; $i -lt $numberOfPairs; $i++) { | |
$maybePair = Read-Host "Pair" | |
$firstNumber, $secondNumber = $maybePair -replace '\(|\)','' -split ',' | |
if (!$firstNumber -or !$secondNumber) { | |
throw "One or both numbers are missing from this pair." | |
} | |
$dummyInt = 0 | |
if (![Int]::TryParse($firstNumber, [ref]$dummyInt) -or ![Int]::TryParse($secondNumber, [ref]$dummyInt)) { | |
throw "One of both of the numbers in this pair are invalid." | |
} | |
if ($secondNumber-$firstNumber -ne 1) { | |
throw "[$firstNumber] and [$secondNumber] do not compose a Ruth-Aaron pair." | |
} | |
$pairs += ,@($firstNumber,$secondNumber) | |
} | |
write-verbose "Pairs: $pairs" | |
foreach ($pair in $pairs) { | |
$firstNumber = $pair[0] | |
$secondNumber = $pair[1] | |
write-host "($firstNumber`,$secondNumber) " -NoNewLine | |
if ( (GetSumOfPrimeFactors($firstNumber)) -ne (GetSumOfPrimeFactors($secondNumber)) ) { | |
write-host "NOT VALID" | |
} | |
else { | |
write-host "VALID" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment