How to Solve Math Puzzle using PowerShell script with Bruteforce Algorithm?


This is a interesting math puzzle:

tex_4724857dbe22ab7059cf271b12f28860 How to Solve Math Puzzle using PowerShell script with Bruteforce Algorithm? algorithms math powershell

X, Y and Z is an integer between 0 to 9. so what are they? We can write a PowerShell script to search for every possible values from 1 to 9, for X, Y and Z respectively.

for ($X = 1; $X -le 9; $X += 1) {// X can't be zero
    for ($Y = 1; $Y -le 9; $Y += 1) { // Y can't be zero either.
        for ($Z = 0; $Z -le 9; $Z += 1) {
            $XY = $X * 10 + $Y;
            $YZZ = $Y * 100 + $Z * 10 + $Z
            if ($XY + $XY -eq $YZZ) { // XY + XY = YZZ
                Write-Host $XY + $XY = $YZZ
            }
        }
    }
}

This outputs: X = 6, Y = 1, and Z = 2. You don’t need to write bruteforce algorithm to search for these 3 values because of a simple logics:

  • X should be larger or equal than 5 otherwise the sum won’t be 3 digits.
  • Y can only be 1 i.e. the maximum two digits 99 + 99 = 198.
  • Y = 1 so Z = 2
  • and X = 6

You may also like: 大白 + 大白 = 白胖胖

powershell-script How to Solve Math Puzzle using PowerShell script with Bruteforce Algorithm? algorithms math powershell

powershell-script

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
287 words
Last Post: Software Engineer Interview Question - How to Improve Dynamic Programming Space Complexity?
Next Post: Chrome Extension to Switch between Simplified Chinese and Traditional Chinese Automatically

The Permanent URL is: How to Solve Math Puzzle using PowerShell script with Bruteforce Algorithm?

Leave a Reply