Part of the series

Several example codes

~1 min read • Updated Oct 4, 2025

Program Overview

This Python program calculates how many four-digit numbers can be formed using the digits 0, 1, 2, 5, 7, 8, without repeating any digit.
The digits in each number must be arranged in strictly descending order (e.g., 8520 is valid, but 2058 is not).
Additionally, numbers starting with 0 are excluded.


Python Code:


from itertools import permutations

# Allowed digits
digits = ['0', '1', '2', '5', '7', '8']
count = 0

# Generate all 4-digit permutations without repetition
for p in permutations(digits, 4):
    number = ''.join(p)
    
    # Check if digits are in descending order
    if list(number) == sorted(number, reverse=True):
        # Exclude numbers starting with zero
        if number[0] != '0':
            count += 1

print("Total descending 4-digit numbers without repetition:", count)

Sample Output:


Total descending 4-digit numbers without repetition: 35

Step-by-Step Explanation:

- permutations generates all 4-digit combinations without repeating digits
- sorted(number, reverse=True) checks if digits are in descending order
- number[0] != '0' ensures the number does not start with zero
- The count variable tracks how many valid numbers are found


Written & researched by Dr. Shahin Siami