Skip to content

Instantly share code, notes, and snippets.

@iampato
Last active March 14, 2023 16:24
Show Gist options
  • Save iampato/bc154155445ac3604883cb01c37a45f1 to your computer and use it in GitHub Desktop.
Save iampato/bc154155445ac3604883cb01c37a45f1 to your computer and use it in GitHub Desktop.
/* *Print out all palindrome and prime numbers between 1 and 100. Use any language you want. 1. Palindrome definition: a word, phrase, or sequence that reads the same backwards as forwards 2. Prime number definition: a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1 (e.g. 2, 3, 5, 7, 11). */
/*
*Print out all palindrome and prime numbers between 1 and 100. Use any language you want.
1. Palindrome definition: a word, phrase, or sequence that reads the same backwards as forwards
2. Prime number definition: a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1 (e.g. 2, 3, 5, 7, 11).
*/
void main() {
// create numbers list from 1 to 100
List<int> numbers = List.generate(100, (int index) => index + 1);
// print(numbers);
// iterate the list
for (int i = 0; i < numbers.length; i++) {
// check of its a prime number to reduce time
if (isPrime(numbers[i])) {
// then check for if the number is a palindrome
// algorithm is reversing and check if they are the same
int? palindrome = isPalindrome(numbers[i]);
if (palindrome != null) {
print(palindrome);
}
}
}
}
int? isPalindrome(int number) {
String numberToString = number.toString();
List<String> numberToList = numberToString.split('').toList();
if (numberToList.reversed.join('') == numberToString) {
return int.parse(numberToString);
} else {
return null;
}
}
bool isPrime(int number) {
if (number < 2) {
return false;
}
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment