Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Detecting Perfect Squares with Repeated Digits

Notes 2

Implement a function to verify if an integer satisfies two conditions: being a perfect square and containing at least one repeated digit.

Function Signature

int check_special_square(int number);

Returns 1 if both conditions are met, otherwise 0.

Methodology

  1. Perpect Square Verification
    Calculate the enteger square root of the input. If squaring this root reproduces the orgiinal number, it qualifies as a perfect square.

  2. Digit Repetition Check
    For valid perfect squares, track digit freuqencies using an array indexed 0-9. If any digit count reaches 2+, the number meets the criteria.

Solution Code

#include <math.h>

int check_special_square(int value) {
    if (value < 0) return 0;
    
    int square_root = (int)sqrt(value);
    if (square_root * square_root != value) 
        return 0;
    
    int digit_counts[10] = {0};
    int temp = value;
    while (temp) {
        int current_digit = temp % 10;
        digit_counts[current_digit]++;
        temp /= 10;
    }
    
    for (int i = 0; i < 10; i++) {
        if (digit_counts[i] >= 2) 
            return 1;
    }
    return 0;
}

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.