Created
October 17, 2015 15:52
-
-
Save JFFail/3fc4c11293d4d63f43d2 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer #235 - Ruth-Aaron pairs
This file contains hidden or 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
#Reddit Daily Programmer #235 | |
#https://www.reddit.com/r/dailyprogrammer/comments/3nkanm/20151005_challenge_235_easy_ruthaaron_pairs/ | |
#Function to figure out the factors. | |
function GetFactors { | |
param([int] $number) | |
$results = @() | |
$counter = 2 | |
while($counter -lt $number) { | |
if(($number % $counter) -eq 0) { | |
$results += $counter | |
} | |
$counter++ | |
} | |
return $results | |
} | |
#Function to figure out if a factor is prime! | |
function FindPrimes { | |
param([int] $number) | |
$isPrime = $true | |
$counter = 2 | |
while($counter -lt $number) { | |
if(($number % $counter) -eq 0) { | |
$isPrime = $false | |
break | |
} | |
$counter++ | |
} | |
return $isPrime | |
} | |
#Function to turn array of factors into array of prime factors. | |
function PrimeArray { | |
param($factorArray) | |
$results = @() | |
foreach($factor in $factorArray) { | |
if(FindPrimes($factor)) { | |
$results += $factor | |
} | |
} | |
return $results | |
} | |
#Function to sum the array of values. | |
function SumNums { | |
param($primeArray) | |
$sum = 0 | |
foreach($item in $primeArray) { | |
$sum += $item | |
} | |
return $sum | |
} | |
#Main code with variables and junk. | |
$firstNum = 77 | |
$secondNum = 78 | |
$firstFactors = GetFactors($firstNum) | |
$secondFactors = GetFactors($secondNum) | |
#Get an array of prime factors for each number. | |
$firstPrimeFactors = PrimeArray($firstFactors) | |
$secondPrimeFactors = PrimeArray($secondFactors) | |
#Now compare the two to find unique values and add them. | |
$firstSum = SumNums($firstPrimeFactors) | |
$secondSum = SumNums($secondPrimeFactors) | |
#See if the sums match! | |
Write-Host "$firstNum, $secondNum > " -NoNewline | |
if($firstSum -eq $secondSum) { | |
Write-Host "VALID" | |
} else { | |
Write-Host "NOT VALID" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment