Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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
- #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
- require 'prime'
- is_palindrome = -> i { i.to_s == i.to_s.reverse }
- puts Prime.lazy.select(&is_palindrome).take(100).to_a
- # 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
- primes.each { |n|
- if is_palindrome?(n) == false
- primes.delete(n)
- end
- }
- primes.each { |n|
- if is_palindrome?(n)
- puts n
- end
- }
- >> 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"]
- primes.delete_if { |n| !is_palindrome?(n) }
- #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
- # >> 2
- # >> 3
- # >> 5
- # >> 7
- # >> 11
- # >> 101
- # >> 131
- # >> 151
- # >> 181
- # >> 191
- # >> 313
- # >> 353
- # >> 373
- # >> 383
- primes.delete_if { |n| !is_palindrome?(n) }
- puts primes.select{ |n| n == n.to_s.reverse.to_i }
- #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 }
- require 'prime'
- e = Prime.each
- 100.times {begin i = e.next end until i.to_s == i.to_s.reverse; puts i}
- 2
- 3
- 5
- 7
- 11
- 101
- 131
- 151
- 181
- 191
- 313
- 353
- 373
- 383
- 727
- ...
- e.class = Prime::EratosthenesGenerator
- i = e.succ
- is_palindrome = -> (i) { i.to_s == i.to_s.reverse }
- def prime?(number)
- flag = true
- (2..number/2).each do |n|
- if (number%n) == 0
- flag = false
- break
- end
- end
- flag
- end
- palindromic_primes = -> (length) do
- 2.upto(Float::INFINITY).lazy.select { |x| is_palindrome.call(x) && prime?(x) }.first(length)
- end
- puts palindromic_primes.(5)
- require 'prime'
- p Prime.each.lazy.select{|x| x.to_s == x.to_s.reverse}.first(100)
Add Comment
Please, Sign In to add comment