Strange plus numbers
n is a strange plus number if the sum of the first two digits is prime and the sum of the second two digits is also prime.
Where 100 < n < 500
Haskell
<lang haskell>import Data.List (intercalate) import Data.List.Split (chunksOf) import Data.Numbers.Primes (isPrime)
STRANGE PLUS NUMBERS -----------------
allStrangePlus :: [Int] allStrangePlus = filter isStrangePlus [100 .. 500]
isStrangePlus :: Int -> Bool isStrangePlus n =
inRange 100 500 n && test digitList && test (tail digitList) where test = isPrime . sum . take 2 digitList = digits n
digits :: Int -> [Int] digits = fmap (read . return) . show
inRange :: Int -> Int -> Int -> Bool inRange low high n = low <= n && high >= n
TEST -------------------------
main :: IO () main =
(putStrLn . intercalate "\n\n") [ "Strange Plus numbers found:", "(total " <> (show . length) allStrangePlus <> ")", "Full list:", unlines (unwords <$> chunksOf 10 (show <$> allStrangePlus)) ]</lang>
- Output:
Strange Plus numbers found: (total 65) Full list: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Python
Using sympy.isprime
<lang python>Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> from sympy import isprime
>>> [x for x in range(101,500)
if isprime(sum(int(c) for c in str(x)[:2])) and isprime(sum(int(c) for c in str(x)[1:]))]
[111, 112, 114, 116, 120, 121, 123, 125, 129, 141, 143, 147, 149, 161, 165, 167, 202, 203, 205, 207, 211, 212, 214, 216, 230, 232, 234, 238, 250, 252, 256, 258, 292, 294, 298, 302, 303, 305, 307, 320, 321, 323, 325, 329, 341, 343, 347, 349, 383, 385, 389, 411, 412, 414, 416, 430, 432, 434, 438, 470, 474, 476, 492, 494, 498] >>> </lang>
REXX
<lang rexx>/*REXX pgm lists strange+ integers (within a range); sum of adjacent dec. digs is prime.*/ parse arg LO HI . /*obtain optional arguments from the CL*/ if LO== | LO=="," then LO= 101 /*Not specified? Then use the default.*/ if HI== | HI=="," then HI= 499 /* " " " " " " */ !.= 0; !.2= 1; !.3= 1; !.5= 1; !.7= 1 /*build array of sums that are prime. */
!.11= 1; !.13= 1; !.17= 1; !.19= 1 /* " " " " " " " */
$= /*the list of strange+ numbers (so far)*/
- = 0 /* " number " " " " " */
do j=LO to HI; L= length(j) /*look for strange+ numbers in range. */ if L==1 then iterate /*Number too short? Then skip it. */
do k=1 for L-1 /*examine the difference in the digits.*/ parse var j =(k) y +1 z +1 /*get two adjacent decimal digits: Y Z */ sum= y + z /*sum of two adjacent decimal digits. */ if \!.sum then iterate j /*Sum not prime? Then skip this number*/ end /*k*/ #= # + 1 /*bump the number of "strange+" numbers*/ $= $ j /*append the number to the $ list. */ end /*j*/ /*stick a fork in it, we're all done. */
say # ' strange plus numbers found between ' LO " and " HI ' (inclusive)' say say strip($)</lang>
- output when using the default inputs:
65 strange plus numbers found between 101 and 499 (inclusive) 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Ring
<lang ring> load "stdlib.ring"
row = 0 see "Strange plus numbers are:"
for n = 100 to 500
flag = 1 str = string(n) for m = 1 to len(str)-1 num1 = number(str[m]) num2 = number(str[m+1]) pr = num1+num2 if not isprime(pr) flag = 0 exit ok next if flag = 1 row = row + 1 if (row-1) % 11 = 0 see nl else see " " + str ok ok
next </lang>
- Output:
Strange plus numbers are: 112 114 116 120 121 123 125 129 141 143 149 161 165 167 202 203 205 207 211 212 216 230 232 234 238 250 252 256 258 292 298 302 303 305 307 320 321 323 325 329 343 347 349 383 385 389 411 412 414 416 432 434 438 470 474 476 492 494 498
Wren
Simple brute force is adequate for this. <lang ecmascript>var primes = [2, 3, 5, 7, 11, 13, 17] var count = 0 var d = [] System.print("Strange plus numbers in the open interval (100, 500) are:\n") for (i in 101..499) {
d.clear() var j = i while (j > 0) { d.add(j % 10) j = (j/10).floor } if (primes.contains(d[0] + d[1]) && primes.contains(d[1] + d[2])) { System.write("%(i) ") count = count + 1 if (count % 10 == 0) System.print() }
} if (count % 10 != 0) System.print() System.print("\n%(count) strange plus numbers in all.")</lang>
- Output:
Strange plus numbers in the open interval (100, 500) are: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 65 strange plus numbers in all.