## Problems writing a script that outputs first 100 palindromic primes

I wrote this script to find the first 100 primes that are also palindromes. For some reason some primes are not being printed (13, 17, etc.), and the palindromic function does not seem to work. Any tips?

#palindrome checker def is_palindrome?(number) number == number.to_s.reverse.to_i end #primes will consists of all primes starting with 2 #i will be incrememented and each i will be checked for being prime primes = [2] i = 3 #we only want the first 100 palindromic primes while primes.length < 100 #test is false for a prime test = false #checks if i is divisible by any of the elements in the array primes.each { |n| if i % n == 0 test = true end } #if i is prime that it is added to the primes array if test == false primes.push(i) end i += 1 end #remove all non palindromic primes primes.each { |n| if is_palindrome?(n) == false primes.delete(n) end } #puts palindromic primes puts primes

I got it to work by changing your script to this:

#palindrome checker def is_palindrome?(number) number == number.to_s.reverse.to_i end #primes will consists of all primes starting with 2 #i will be incrememented and each i will be checked for being prime primes = [2] i = 3 #we only want the first 100 palindromic primes while primes.length < 100 #test is false for a prime test = false #checks if i is divisible by any of the elements in the array primes.each { |n| if i % n == 0 test = true end } #if i is prime that it is added to the primes array if test == false primes.push(i) if is_palindrome?(i) # This is the line I changed end i += 1 end #puts palindromic primes puts primes

You were getting weird errors by modifying and iterating over your the `primes`

array at the same time. I moved the palindrome check to inside the while loop so you will actually get 100 palindromes vs. 100 primes minus the non-palindromes.

**Palindromic Primes,** Given a number n, print all palindromic primes smaller than or equal to n. For example, If n is 10, the output should be “2, 3, 5, 7′. And if n is 20, the output should This statement/ mainly compares the first. // digit with the last digit Driver Code. n = 100 ;. print ( "Palindromic primes smaller" ,. "than or equal to" , n, "are :" );. Write a program that loops over the first 100,000 positive integers and displays the integer if it is a palindromic prime number (see PE6)- Your program should include, at a minimum, a method that determines whether or not a number is prime and a method that determines whether or not a number is palindromic. Here are the first few values in your output: Palindromic primes 2 181 373 787 10301 10501 10601 11311 3 101 191 383 797 5 131 313 727 919 7 151 353 757 929

Here's how I'd write it:

require 'prime' is_palindrome = -> i { i.to_s == i.to_s.reverse } puts Prime.lazy.select(&is_palindrome).take(100).to_a

Simple, clear, concise.

The output is:

# 2 # 3 # 5 # 7 # 11 # 101 # 131 # 151 # 181 # 191 # 313 # 353 # 373 # 383 # 727 # 757 # 787 # 797 # 919 # 929 # 10301 # 10501 # 10601 # 11311 # 11411 # 12421 # 12721 # 12821 # 13331 # 13831 # 13931 # 14341 # 14741 # 15451 # 15551 # 16061 # 16361 # 16561 # 16661 # 17471 # 17971 # 18181 # 18481 # 19391 # 19891 # 19991 # 30103 # 30203 # 30403 # 30703 # 30803 # 31013 # 31513 # 32323 # 32423 # 33533 # 34543 # 34843 # 35053 # 35153 # 35353 # 35753 # 36263 # 36563 # 37273 # 37573 # 38083 # 38183 # 38783 # 39293 # 70207 # 70507 # 70607 # 71317 # 71917 # 72227 # 72727 # 73037 # 73237 # 73637 # 74047 # 74747 # 75557 # 76367 # 76667 # 77377 # 77477 # 77977 # 78487 # 78787 # 78887 # 79397 # 79697 # 79997 # 90709 # 91019 # 93139 # 93239 # 93739 # 94049

**Palindromic Primes (for 100 instances); working code, looking for ,** Palindromic Primes (for 100 instances); working code, looking for pointers on to print the first 100 instances of palindromic prime numbers (eg 383, 90709) - prime For example, in this code, which option is faster, the commented option or the I went through the web scraping phase, made simple scripts to move files out 0 Problems writing a script that outputs first 100 palindromic primes Jun 19 '18. View all questions and answers → Badges (9) Gold —

Your problem is here:

primes.each { |n| if is_palindrome?(n) == false primes.delete(n) end }

You're iterating over `primes`

, and inside the block modifying it. That's very error prone.

Try doing:

primes.each { |n| if is_palindrome?(n) puts n end }

Also, you could write your code a lot better, to make it more readable, and you could use a more efficient algorithm to calculate the prime numbers, like the Sieve of Eratosthenes.

**Emirp primes,** CALL EMIRP(10,10000,10000, 1,1000000) !Of three separate invocations. END !Whee! Output: Show the first 1 to 20 emirP numbers ( Palindromic primes smaller than or equal to 100 are : 2 3 5 7 11 This article is contributed by Rahul Agrawal .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org.

I'm not sure about the internal workings, but deleting elements from an array over which you are iterating will cause it to skip things. E.g.:

>> arr = %w{John Paul George Ringo} => ["John", "Paul", "George", "Ringo"] >> # Print each one out; delete if it contains "o" >> arr.each {|e| puts e; arr.delete e if e =~ /o/} John George => ["Paul", "Ringo"]

didn't even evaluate `Paul`

or `Ringo`

.

You should use Array#delete_if:

primes.delete_if { |n| !is_palindrome?(n) }

However, more specifically to your question, you can just put the `is_palindrome?`

check in the loop which builds your array, eliminating the need for the second filtering loop (and allowing you to build the list of the 100 palindromic primes you want).

**Write a program to print prime numbers between 1 to 100,** There is a minute mistake in this program, Outer for-loop is running for 1 to n and due to this 1 is Duration: 9:09
Posted: Dec 12, 2016 Palindromic Prime Numbers Write a program that finds the first palindromic prime number above 100,000. Prime Numbers Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers.

Your code needs some TLC. Here's how I'd do it:

#palindrome checker def is_palindrome?(number) number == number.to_s.reverse.to_i end #primes will consists of all primes starting with 2 #i will be incrememented and each i will be checked for being prime primes = [2] i = 3 #we only want the first 100 palindromic primes while primes.length < 100 #checks if i is divisible by any of the elements in the array primes << i unless primes.any? { |n| i % n == 0 } i += 1 end #remove all non palindromic primes primes.delete_if { |n| is_palindrome?(n) == false } #puts palindromic primes puts primes

Which, when run, outputs:

# >> 2 # >> 3 # >> 5 # >> 7 # >> 11 # >> 101 # >> 131 # >> 151 # >> 181 # >> 191 # >> 313 # >> 353 # >> 373 # >> 383

`primes.delete_if { |n| is_palindrome?(n) == false }`

could be written more succinctly as:

primes.delete_if { |n| !is_palindrome?(n) }

Or, do away with stripping `primes`

and calling `is_palindrome?`

and use a simple `reject`

:

puts primes.select{ |n| n == n.to_s.reverse.to_i }

which reduces the code to:

#primes will consists of all primes starting with 2 #i will be incrememented and each i will be checked for being prime primes = [2] i = 3 #we only want the first 100 palindromic primes while primes.length < 100 #checks if i is divisible by any of the elements in the array primes << i unless primes.any? { |n| i % n == 0 } i += 1 end #puts palindromic primes puts primes.select{ |n| n == n.to_s.reverse.to_i }

**Solved: A Program That Displays The First 100 Palindromic ,** Answer to a program that displays the first 100 palindromic prime numbers you have created using the This problem has been solved! For example, 131 is a prime and also a palindromic prime. Online Tutoring · Solutions Manual · Study 101 · Test Prep · Textbook Rental · Used Textbooks · Digital Access Codes Efficient Approach: We start from 1 and create palindromes of odd digit and even digits up to n. For every number (starting from 1), we append its reverse at end if we need even length palindrome numbers. For odd length palindrome, we append reverse of all digits except last one.

Java Numbers: Exercise-24 with Solution. Write a Java program to check if a number is palindrome or not. In number system a palindromic number is a number that is the same when written forwards or backwards, i.e., of the form.

Output: Prime numbers from 1 to 100 are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Program to display prime numbers from 1 to n. It will display all the prime numbers between 1 and n (n is the number, entered by user).

A palindromic prime is any prime number that is also a palindrome. For example, 131 is a palindromic prime, as are 313 and 757. Write a Java program that displays the first 100 palindromic prime numbers. Display 10 numbers per line with a spacing of 6; all numbers should be left-aligned.

##### Comments

- In what way does it not seem to work? Why would it print those primes (not being palindromes)? Also, it looks like it will get the first 100 primes, then remove those which are not palindromes (leaving you with fewer than 100 in the list).
- It doesn't work in that many primes are included that aren't palindromes. You're right about the second part, I noticed that after submitting the question. @iamnotmaynard
- Nice use of
`lazy`

, Jörg. +1 - Beautiful thing I've learned here!
`take`

and`select`

working together lazily. - You can remove
`begin..end`

.. Is it needed ? I don't think so.`i = e.next until i.to_s == i.to_s.reverse`

is Ok. - The problem is that
`i = e.next`

must be executed first. Try it. I'd love to find a further simplification. - Then place one line
`i = e.next`

before,`#times`

. Then no need`begin..end`

. - I considered that, but don't like it as well.