Guest User

Untitled

a guest
Jun 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #palindrome checker
  2. def is_palindrome?(number)
  3. number == number.to_s.reverse.to_i
  4. end
  5.  
  6.  
  7. #primes will consists of all primes starting with 2
  8. #i will be incrememented and each i will be checked for being prime
  9. primes = [2]
  10. i = 3
  11.  
  12. #we only want the first 100 palindromic primes
  13. while primes.length < 100
  14. #test is false for a prime
  15. test = false
  16. #checks if i is divisible by any of the elements in the array
  17. primes.each { |n|
  18. if i % n == 0
  19. test = true
  20. end
  21. }
  22. #if i is prime that it is added to the primes array
  23. if test == false
  24. primes.push(i)
  25. end
  26. i += 1
  27. end
  28.  
  29. #remove all non palindromic primes
  30. primes.each { |n|
  31. if is_palindrome?(n) == false
  32. primes.delete(n)
  33. end
  34. }
  35.  
  36. #puts palindromic primes
  37. puts primes
  38.  
  39. #palindrome checker
  40. def is_palindrome?(number)
  41. number == number.to_s.reverse.to_i
  42. end
  43.  
  44.  
  45. #primes will consists of all primes starting with 2
  46. #i will be incrememented and each i will be checked for being prime
  47. primes = [2]
  48. i = 3
  49.  
  50. #we only want the first 100 palindromic primes
  51. while primes.length < 100
  52. #test is false for a prime
  53. test = false
  54. #checks if i is divisible by any of the elements in the array
  55. primes.each { |n|
  56. if i % n == 0
  57. test = true
  58. end
  59. }
  60. #if i is prime that it is added to the primes array
  61. if test == false
  62. primes.push(i) if is_palindrome?(i) # This is the line I changed
  63. end
  64. i += 1
  65. end
  66.  
  67.  
  68. #puts palindromic primes
  69. puts primes
  70.  
  71. require 'prime'
  72.  
  73. is_palindrome = -> i { i.to_s == i.to_s.reverse }
  74. puts Prime.lazy.select(&is_palindrome).take(100).to_a
  75.  
  76. # 2
  77. # 3
  78. # 5
  79. # 7
  80. # 11
  81. # 101
  82. # 131
  83. # 151
  84. # 181
  85. # 191
  86. # 313
  87. # 353
  88. # 373
  89. # 383
  90. # 727
  91. # 757
  92. # 787
  93. # 797
  94. # 919
  95. # 929
  96. # 10301
  97. # 10501
  98. # 10601
  99. # 11311
  100. # 11411
  101. # 12421
  102. # 12721
  103. # 12821
  104. # 13331
  105. # 13831
  106. # 13931
  107. # 14341
  108. # 14741
  109. # 15451
  110. # 15551
  111. # 16061
  112. # 16361
  113. # 16561
  114. # 16661
  115. # 17471
  116. # 17971
  117. # 18181
  118. # 18481
  119. # 19391
  120. # 19891
  121. # 19991
  122. # 30103
  123. # 30203
  124. # 30403
  125. # 30703
  126. # 30803
  127. # 31013
  128. # 31513
  129. # 32323
  130. # 32423
  131. # 33533
  132. # 34543
  133. # 34843
  134. # 35053
  135. # 35153
  136. # 35353
  137. # 35753
  138. # 36263
  139. # 36563
  140. # 37273
  141. # 37573
  142. # 38083
  143. # 38183
  144. # 38783
  145. # 39293
  146. # 70207
  147. # 70507
  148. # 70607
  149. # 71317
  150. # 71917
  151. # 72227
  152. # 72727
  153. # 73037
  154. # 73237
  155. # 73637
  156. # 74047
  157. # 74747
  158. # 75557
  159. # 76367
  160. # 76667
  161. # 77377
  162. # 77477
  163. # 77977
  164. # 78487
  165. # 78787
  166. # 78887
  167. # 79397
  168. # 79697
  169. # 79997
  170. # 90709
  171. # 91019
  172. # 93139
  173. # 93239
  174. # 93739
  175. # 94049
  176.  
  177. primes.each { |n|
  178. if is_palindrome?(n) == false
  179. primes.delete(n)
  180. end
  181. }
  182.  
  183. primes.each { |n|
  184. if is_palindrome?(n)
  185. puts n
  186. end
  187. }
  188.  
  189. >> arr = %w{John Paul George Ringo}
  190. => ["John", "Paul", "George", "Ringo"]
  191. >> # Print each one out; delete if it contains "o"
  192. >> arr.each {|e| puts e; arr.delete e if e =~ /o/}
  193. John
  194. George
  195. => ["Paul", "Ringo"]
  196.  
  197. primes.delete_if { |n| !is_palindrome?(n) }
  198.  
  199. #palindrome checker
  200. def is_palindrome?(number)
  201. number == number.to_s.reverse.to_i
  202. end
  203.  
  204.  
  205. #primes will consists of all primes starting with 2
  206. #i will be incrememented and each i will be checked for being prime
  207. primes = [2]
  208. i = 3
  209.  
  210. #we only want the first 100 palindromic primes
  211. while primes.length < 100
  212.  
  213. #checks if i is divisible by any of the elements in the array
  214. primes << i unless primes.any? { |n| i % n == 0 }
  215.  
  216. i += 1
  217. end
  218.  
  219. #remove all non palindromic primes
  220. primes.delete_if { |n| is_palindrome?(n) == false }
  221.  
  222. #puts palindromic primes
  223. puts primes
  224.  
  225. # >> 2
  226. # >> 3
  227. # >> 5
  228. # >> 7
  229. # >> 11
  230. # >> 101
  231. # >> 131
  232. # >> 151
  233. # >> 181
  234. # >> 191
  235. # >> 313
  236. # >> 353
  237. # >> 373
  238. # >> 383
  239.  
  240. primes.delete_if { |n| !is_palindrome?(n) }
  241.  
  242. puts primes.select{ |n| n == n.to_s.reverse.to_i }
  243.  
  244. #primes will consists of all primes starting with 2
  245. #i will be incrememented and each i will be checked for being prime
  246. primes = [2]
  247. i = 3
  248.  
  249. #we only want the first 100 palindromic primes
  250. while primes.length < 100
  251.  
  252. #checks if i is divisible by any of the elements in the array
  253. primes << i unless primes.any? { |n| i % n == 0 }
  254.  
  255. i += 1
  256. end
  257.  
  258. #puts palindromic primes
  259. puts primes.select{ |n| n == n.to_s.reverse.to_i }
  260.  
  261. require 'prime'
  262.  
  263. e = Prime.each
  264. 100.times {begin i = e.next end until i.to_s == i.to_s.reverse; puts i}
  265.  
  266. 2
  267. 3
  268. 5
  269. 7
  270. 11
  271. 101
  272. 131
  273. 151
  274. 181
  275. 191
  276. 313
  277. 353
  278. 373
  279. 383
  280. 727
  281. ...
  282.  
  283. e.class = Prime::EratosthenesGenerator
  284.  
  285. i = e.succ
  286.  
  287. is_palindrome = -> (i) { i.to_s == i.to_s.reverse }
  288.  
  289. def prime?(number)
  290. flag = true
  291.  
  292. (2..number/2).each do |n|
  293. if (number%n) == 0
  294. flag = false
  295. break
  296. end
  297. end
  298.  
  299. flag
  300. end
  301.  
  302. palindromic_primes = -> (length) do
  303. 2.upto(Float::INFINITY).lazy.select { |x| is_palindrome.call(x) && prime?(x) }.first(length)
  304. end
  305.  
  306. puts palindromic_primes.(5)
  307.  
  308. require 'prime'
  309. p Prime.each.lazy.select{|x| x.to_s == x.to_s.reverse}.first(100)
Add Comment
Please, Sign In to add comment