How to find nth palindrome number using if-else where n < 100 - palindrome

How can I find nth palindrome number using if-else where n < 100.Is there any way to do it in O(1)?

Related

sorting numbers

How to generate a combination of 5 elements out of 8 elements where the final list should have only the combination of 5 elements which are two or more elements different from each other?
I started replacing the elements with numbers from 1-8 and generated the initial list using:
x = 1:8;
v = permn(x,5);
The initial list consists 8^5 numbers. e. g. 11111,11112,11113.....88888.
If I take the starting seed as 11111, the sorted numbers can be like, 11122, 11123 and so on. Because these numbers are atleast two element difference with 11111. But then from this second list, both 11122 and 11123 can't go to the next list because they have only one element difference.
The final list should have all these unique set of elements which has atleast two or more element difference with each other.
Can someone please help me to implement the condition?
An iterative solution have and order o(n^2) with n=size(v,1):
m=1
while m < size(v,1)
part2 = v(m+1:end, :);
%compare each row with the following rows e.g. part2.
u = bsxfun(#ne, v(m,:), part2);
%check if number of different elements greater than 1
s = sum(u,2) > 1;
%extract those rows and append to the current rows
v= [v(1:m,:); part2(s,:)];
m = m + 1;
end
The final v will have unique elements.
Create a bubble sort with inner and outer loop, compare elements from inner loop and outer loop based on rank (rank = 1 if two or more elements differ) thereafter swap elements if rank <> 0.
Pseudo Code
For I= 0 to (N-1)
For J= I+1 to N
If Rank(Array(I), Array(J)) <> 0 Then
Swap(Array(I), Array(J))
End
End
End

MATLAB - How do I find the first integer of an infinite set that satisfies this condition?

I want to find the smallest integer P, such that the number of primes in the set {1,2,..., P} is less than P/6.
I think have the answer via (long) trial and error but would like to know how to verify this through MATLAB.
You can use isprime to check if any value in an array is a prime number. If we want to check all integers up until the integer N we can do
% You can change this to the maximum number that you'd like to consider for P
N = 2000;
possible_P_values = 2:N; % We omit 1 here since it's not a prime number
primes = isprime(possible_P_values);
To determine how many primes have occured up to a given integer N we can use cumsum of this logical matrix (the cumulative sum)
nPrimes_less_than_or_equal_to_P = cumsum(primes);
Then we can divide possible_P_values by 6 and check where the number of primes up to a certain point is less than that number.
is_less_than_P_over_6 = nPrimes_less_than_or_equal_to_P < (possible_P_values ./ 6);
Then we can identify the first occurance with find
possible_P_values(find(is_less_than_P_over_6, 1, 'first'))
% 1081

How to check my matrix if its divisible by 12

im new to matlab and i've run into a slight problem. I want to check my matrix that generated random number if they are divisible by 12. Then i want to list number of digit divisible by 12 and the total sum of those.
clc
clear
format compact
a=4
b=0
N=50+a
R=randi([100+a,159+b], 1, N) % generate random no. from 100+a to 159 on a matrix 1xN
s1=0
N1=0
for i = 1
for j= 1:N
if rem(R,12)==0
N1=N1+1;
s1=s1+R(i,j);
else
N1=N1+0;
s1=s1+0;
end
end
end
numberof1=N1
sum1=s1
Your code isn't working because your are calling rem(R, 12) (remainder of all elements) as opposed to the remainder of the specific element (rem(R(i,j), 12)).
The better approach though would be to remove the for loop and generate a logical matrix the size of R that is true when that number is divisible by 12 and false otherwise by passing the entire matrix to rem.
is_divisible_by_12 = rem(R, 12) == 0;
Then we can use this to compute the sum of these by using this logical array as an index into R
subset = R(is_divisible_by_12);
number = numel(subset);
s1 = sum(subset);

How does this prime number generation method work?

I want to figure out what the background of this code is, specifically, the reason of the commands written. I understand everything until the first line of the for loop, but then I got lost with the second line:
N= input('Enter your number: ');
primes = 2:N;
p=2;
while (p <= N)
for i = 2*p:p:N
primes(i - 1) = 0;
end;
p = p + 1;
end
primes = primes(primes > 0)
Can someone help me to understand this code please?
The code implements the Sieve of Eratosthenes to find the primes numbers.
It generated the array primes which contains the integer numbers from 2 to N
primes = 2:N
The while loop iterates across the integer values
while (p <= N)
At each iteration of the while loop in the for loop, the multiples of the current value of p are generated within the definition of the set of values of the loop index variable i
2*p:p:N
at each iteration of the for loop the element of the primes array in the position i is set to 0
At the end of the for loop, all the multiples of the current value of p are then set to 0
primes(i - 1) = 0;
The process is then repeated for all the integers values between 2 and N by the while loop.
At the end of the while loop the array primes will contains the prime numbers between 2 and N.
primes = primes(primes > 0)
This instruction finds the numbers different from 0 in the array primes and re-defines the array itself by assigning to it all all the numbers different from 0 or, that is the same. it removes from the array primes all of the 0s.
By definition the multiple of a given number is not a prime number.
Hope this helps.

How to find a second number by having first number and distance between these 2 numbers

I am so new to maple and I dont know what to do exactly! I have 10 numbers, for finding k nearest number to any of this number I need to keep distances between all numbers and sort them, based on these distances I can get which x is the nearest x to current number so:
for i from 1 to 10 do
for j from 1 to 10 do
dist[j] := abs(x[i]-x[j]);
result[i,j] := abs(x[i]-x[j]);
end do;
end do;
for h from 1 to 10 do
for k from 1 to 10 do
arr[k] := result[h,k];
end do;
distances := (quicksort(arr,1,10));
for t from 1 to 10 do
sortedMatrix[h,t] := distances[t];
end do;
end do;
print(sortedMatrix);
Now I have distances and a number, but i dont know what the other number is?
If I understand correctly, you start with an array of N elements and you want, for each of those N elements, the nearest neighbour in that array.
My approach would be to just sort the array first and then loop over it, setting each elements nearest neighbour equal to its neighbour in the sorted array.
Example: suppose you have the array [5,1,86,54,23,46]. If you sort that, you get [1,5,23,46,54,86]. To find the nearest neighbours, you go over the array and look at both elements adjacent. For example, 46 has 23 and 54 as neighbouring elements in the sorted array. 45 is closest to 54 so that's his neighbour.
There is no need to calculate all distances (n*n differences) between elements, you only need the distances between neighbouring nodess in the sorted array (2*n differences).
You can expand that for k nearest neighbours by looking to the k nearest neighbours in the sorted array (2*k*n differences).