palindromic numbers
By : user2881871
Date : March 29 2020, 07:55 AM
I wish this helpful for you Here is working code: , You want to flatten only one level, so use flatten(1) instead. code :
(10..14).map { |a|
(a..14).map { |b|
[a, b, a * b, '=']
}
}.flatten(1).select { |v|
v[2].to_s == v[2].to_s.reverse
}
(10..14).flat_map { |a|
(a..14).map { |b|
[a, b, a * b, '=']
}
}.select { |v|
v[2].to_s == v[2].to_s.reverse
}
|
How to find out all palindromic numbers
By : Stratos
Date : March 29 2020, 07:55 AM
wish help you to fix your issue A palindromic number or numeral palindrome is a "symmetrical" number like 16461, that remains the same when its digits are reversed. , Generating all palindromes up to a specific limit. code :
public static Set<Integer> allPalindromic(int limit) {
Set<Integer> result = new HashSet<Integer>();
for (int i = 0; i <= 9 && i <= limit; i++)
result.add(i);
boolean cont = true;
for (int i = 1; cont; i++) {
StringBuffer rev = new StringBuffer("" + i).reverse();
cont = false;
for (String d : ",0,1,2,3,4,5,6,7,8,9".split(",")) {
int n = Integer.parseInt("" + i + d + rev);
if (n <= limit) {
cont = true;
result.add(n);
}
}
}
return result;
}
public static boolean isPalindromic(String s, int i, int j) {
return j - i < 1 || s.charAt(i) == s.charAt(j) && isPalindromic(s,i+1,j-1);
}
public static boolean isPalindromic(int i) {
String s = "" + i;
return isPalindromic(s, 0, s.length() - 1);
}
public static boolean isPalindromic(int i) {
int len = (int) Math.ceil(Math.log10(i+1));
for (int n = 0; n < len / 2; n++)
if ((i / (int) Math.pow(10, n)) % 10 !=
(i / (int) Math.pow(10, len - n - 1)) % 10)
return false;
return true;
}
|
palindromic numbers in python
By : Bob Salisbury
Date : March 29 2020, 07:55 AM
wish of those help Trying to find the largest palindrome that's the product of two three-digit numbers. Before I look up the infinitely more efficient and - more importantly - working solution, could you tell me what's wrong with my code? I just keep getting the empty set. , You have 3 problems. Problem 1: Returning early. code :
while n<=999:
while m<=999:
prod = n * m
if str(prod) == str(prod)[::-1] and prod > palind[0]:
palind.pop(0)
palind.append(prod)
# Here
return palind
m = m + 1
n = n + 1
# And here
return palind
while n<=999:
while m<=999:
prod = n * m
# Here v
if str(prod) == str(prod)[::-1] and prod > palind[0]:
palind.pop(0)
palind.append(prod)
m = m + 1
n = n + 1
return palind
palind = None
while n<=999:
while m<=999:
prod = n * m
if str(prod) == str(prod)[::-1]:
if palind is None or prod > palind:
palind = prod
m = m + 1
n = n + 1
return palind
palind = None
for n in xrange(100, 1000):
for m in xrange(100, 1000):
prod = n * m
if str(prod) == str(prod)[::-1]:
if palind is None or prod > palind:
palind = prod
return palind
|
More efficient algorithm for printing numbers that are palindromic and their power to 2 are palindromic too
By : user1055003
Date : March 29 2020, 07:55 AM
should help you out Instead of checking very number for "palindromness", it may be better to iterate through palindromes only. For that just iterate over the first halves of the number and then compose palindrome from it. code :
for(int half=10;half<=99;++half)
{
const int candidate=half*100+Reverse(half);//may need modification for odd number of digits
if(IsPalindrome(candidate*candidate))
Output(candidate);
}
|
Why isn't Matlab returning palindromic numbers?
By : Lyu
Date : March 29 2020, 07:55 AM
should help you out I have created the following code: , Try this:
|