Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to check if a number can be made prime by deleting a single digit in Rust

1 Answer

0 votes
fn remove_the_n_digit(num: i32, n: usize) -> i32 {
    let num_str = num.to_string();
    let result_str = format!("{}{}", &num_str[..n], &num_str[n + 1..]);
    
    result_str.parse::<i32>().unwrap()
}

fn is_prime(n: i32) -> bool {
    if n < 2 || (n % 2 == 0 && n != 2) {
        return false;
    }

    let count = (n as f64).sqrt() as i32;
    for i in (3..=count).step_by(2) {
        if n % i == 0 {
            return false;
        }
    }

    true
}

fn main() {
    let n = 78919;
    let total_digits = n.to_string().len();

    for i in 0..total_digits {
        let tmp = remove_the_n_digit(n, i);
        if is_prime(tmp) {
            println!("yes number = {}", tmp);
            break;
        }
    }
}


  
/*
run:

yes number = 7919

*/

 



answered Sep 28, 2024 by avibootz

Related questions

...