How the Wizard Fought the Monster – A Number Story (with Code)

How the Wizard Fought the Monster – A Number Story

A story of a wizard who fought a monster made of numbers

Once upon a time in the magical land of Integers,
a wise wizard had to defeat a monster made of numbers!
The monster’s power was just a number – but a very stubborn one. ๐Ÿ˜ฎ

To win, the wizard had to bring the number down to zero.

๐Ÿงพ The Monster's Rule

  • If the number is even → use Magic Attackdivide it by 2

  • If the number is odd → use Basic Attacksubtract 1

Let’s say the monster’s number is 14.
Here’s how the battle goes:

Step 0: Monster = 14 (even) → Magic Attack → 7  

Step 1: Monster = 7 (odd) → Basic Attack → 6  

Step 2: Monster = 6 (even) → Magic Attack → 3  

Step 3: Monster = 3 (odd) → Basic Attack → 2  

Step 4: Monster = 2 (even) → Magic Attack → 1  

Step 5: Monster = 1 (odd) → Basic Attack → 0  

๐Ÿ’ฅ Victory in 6 steps!


๐Ÿ’ป Wizard's Code in Python

def number_of_steps(num):
    steps = 0
    while num > 0:
        if num % 2 == 0:
            num = num // 2  # Magic attack (even)
        else:
            num -= 1        # Basic attack (odd)
        steps += 1
    return steps


๐Ÿ” What it does (in simple words):
Start with a number.

If it’s even, divide it by 2.

If it’s odd, subtract 1.

Count how many steps it takes to get to 0.

๐Ÿ’ป Wizard's Code in C#

public class Solution { public int NumberOfSteps(int num) { int steps = 0; while (num > 0) { if (num % 2 == 0) num /= 2; // Magic attack else num -= 1; // Basic attack steps++; } return steps; } }

๐ŸŽฎ Turn This Into a Game!

This logic can be a fun game mechanic:

  • ๐Ÿ’– Monster's HP is a number

  • ✨ Even HP → Magic Attack (costs mana, powerful)

  • ๐Ÿ—ก️ Odd HP → Basic Attack (free, but weak)

  • ⏳ Goal: Defeat monster with minimum steps or in limited time!

Players need to think ahead and pick the smartest move based on the number!



Code inspired by algorithm problem #1342 – Number of Steps to Reduce a Number to Zero

Reference: 1342. Number of Steps to Reduce a Number to Zero @LeetCode


๐Ÿ Python Full Version (with Input & Output)

def number_of_steps(num): steps = 0 print(f"Starting the battle! Monster = {num}") while num > 0: if num % 2 == 0: print(f"{num} is even → Magic Attack → {num // 2}") num = num // 2 else: print(f"{num} is odd → Basic Attack → {num - 1}") num -= 1 steps += 1 print(f"Monster defeated in {steps} steps!") return steps # Try it out monster_number = 14 number_of_steps(monster_number)


๐Ÿ’ป C# Full Version (Console Application Style)

using System; public class Program { public static void Main() { int monster = 14; int steps = 0; Console.WriteLine($"Starting the battle! Monster = {monster}"); while (monster > 0) { if (monster % 2 == 0) { Console.WriteLine($"{monster} is even → Magic Attack → {monster / 2}"); monster /= 2; } else { Console.WriteLine($"{monster} is odd → Basic Attack → {monster - 1}"); monster -= 1; } steps++; } Console.WriteLine($"Monster defeated in {steps} steps!"); } }



Comments