Count Good Numbers - Problem

A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7).

For example, "2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, "3245" is not good because 3 is at an even index but is not even.

Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 10^9 + 7.

A digit string is a string consisting of digits 0 through 9 that may contain leading zeros.

Input & Output

Example 1 — Small Case
$ Input: n = 1
Output: 5
💡 Note: Single digit at even position 0. Valid digits: 0,2,4,6,8. Total = 5.
Example 2 — Two Positions
$ Input: n = 2
Output: 20
💡 Note: Position 0 (even): 5 choices, Position 1 (odd): 4 choices. Total = 5 × 4 = 20.
Example 3 — Larger Input
$ Input: n = 50
Output: 564908303
💡 Note: Even positions: 25, Odd positions: 25. Result = 5²⁵ × 4²⁵ mod (10⁹ + 7).

Constraints

  • 1 ≤ n ≤ 1015

Visualization

Tap to expand
Count Good Numbers: Position-Based Digit RulesExample: n=4 (positions 0,1,2,3)0123Even posOdd posEven posOdd posEven Position Digits0, 2, 4, 6, 8 (5 choices)Odd Position Digits2, 3, 5, 7 (4 choices)Formula: 5^even_positions × 4^odd_positionsResult for n=4: 5² × 4² = 25 × 16 = 400
Understanding the Visualization
1
Input
String length n=4
2
Position Rules
Even positions need even digits, odd positions need prime digits
3
Output
Total count of valid strings
Key Takeaway
🎯 Key Insight: Each position's digit choices are independent - multiply the possibilities!
Asked in
Google 25 Amazon 18 Microsoft 15
32.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen