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

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Semrush - keyword research tool

Linux Foundation Training and Certification

Teach Your Child To Read

Disclosure: My content contains affiliate links.

32,304 questions

42,479 answers

573 users

How to check if a number is circular prime (cyclically rotate left will also be prime) in Dart

1 Answer

0 votes
import 'dart:io';

bool is_prime(int n) {
    if (n == 2) {
        return true;
    }
    if (n < 2 || n % 2 == 0) {
        return false;
    }
    for (var i = 3; i * i <= n; i += 2) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
    
int cyclically_rotate_left(int n) {
    var m = n;
    var p = 1;
    
    while (m >= 10) {
        p *= 10;
        m = m ~/ 10;
    }
    return (m + 10 * (n % p));
}
    
bool is_circular_prime(int n) {
    if (!is_prime(n)) {
        return false;
    }
    
    var rotated_n = cyclically_rotate_left(n);
    
    while (rotated_n != n) {
        if (rotated_n < n || !is_prime(rotated_n)) {
            return false;
        }
        rotated_n = cyclically_rotate_left(rotated_n);
    }
    return true;
}

void main() {
    
    // 3779 = prime
    // 7793 = prime
    // 7937 = prime
    // 9377 = prime

    stdout.write(is_circular_prime(3779));
}




/*
run:

true

*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered Mar 24, 2023 by avibootz
...