Strange numbers: Difference between revisions
(→{{header|REXX}}: added the computer programming language REXX.) |
m (→{{header|REXX}}: added the range of the "strange" numbers to the output.) |
||
Line 133: | Line 133: | ||
end /*j*/ |
end /*j*/ |
||
/*stick a fork in it, we're all done. */ |
/*stick a fork in it, we're all done. */ |
||
say words($) |
say words($) ' strange numbers found between ' LO " and " HI ' (inclusive):'; say |
||
say strip($)</lang> |
|||
say strip($) /* " " list " " " */</lang> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
87 strange numbers found: |
87 strange numbers found between 100 and 500 (inclusive): |
||
130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 |
130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 |
Revision as of 23:03, 22 February 2021
n is a strange number if every adjacent digit differs from its neighbour by prime number.
Let 100<n<500
ALGOL W
<lang algolw>begin % find "strange numbers" - numbers where digits differ from the next %
% by a prime % % returns true if n is strange, false otherwise % logical procedure isStrange( integer value n ) ; begin integer rest; rest := abs( n ); if rest < 10 then false else begin logical strange; integer d0; strange := true; d0 := rest rem 10; rest := rest div 10; while strange and rest > 0 do begin integer d1, diff; d1 := rest rem 10; rest := rest div 10; diff := abs( d1 - d0 ); strange := diff = 2 or diff = 3 or diff = 5 or diff = 7; d0 := d1 end while_strange_and_rest_gt_0 ; strange end if_rest_lt_10__ end isStrange ; % test the isStrange procedure on values 100-499 % begin integer strangeCount; strangeCount := 0; for n := 100 until 499 do begin; if isStrange( n ) then begin strangeCount := strangeCount + 1; if strangeCount rem 20 = 1 then write( i_w := 4, s_w := 0, n ) else writeon( i_w := 4, s_w := 0, n ) end if_isStrange_n end for_n end
end.</lang>
- Output:
130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 242 246 247 249 250 252 253 257 258 270 272 274 275 279 292 294 296 297 302 303 305 307 313 314 316 318 350 352 353 357 358 361 363 364 368 369 381 383 385 386 413 414 416 418 420 424 425 427 429 461 463 464 468 469 470 472 474 475 479 492 494 496 497
Pascal
simple brute force, by adding a prime difference from digit to digit. <lang pascal>program strangenumbers; type
tDeltaIdx = 0..7;
const
dgtCnt = 3; deltaprime : array[tDeltaIdx] of Int32 =(-7,-5,-3,-2,2,3,5,7);
//globals are set to 0 var
Digits : array[0..dgtCnt-1] of Int32; Actdgt,i,j,k,Cnt : Int32;
procedure OutPut; Begin
write(Digits[0],Digits[1],Digits[2],' ');
end;
Begin
For i := 1 to 4 do Begin Digits[0] := i; For k in tDeltaIdx do Begin if (i+deltaprime[k]) in [0..9] then Begin Actdgt := i+deltaprime[k]; Digits[1]:= Actdgt; For j in tDeltaIdx do if (Actdgt+deltaprime[j]) in [0..9] then Begin Digits[2]:= Actdgt+deltaprime[j]; OutPut; inc(cnt); If cnt MOD 20 = 0 then writeln; end; end; end; end; writeln; Writeln('Count : ',cnt);
end. </lang>
- Output:
130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 242 246 247 249 250 252 253 257 258 270 272 274 275 279 292 294 296 297 302 303 305 307 313 314 316 318 350 352 353 357 358 361 363 364 368 369 381 383 385 386 413 414 416 418 420 424 425 427 429 461 463 464 468 469 470 472 474 475 479 492 494 496 497 Count : 87
Raku
<lang perl6># 20210222 Raku programming solution
sub infix:<′>(\x,\y) { abs(x -y) ∈ [2, 3, 5, 7] ?? True !! False }
for (101..499) { .say if so all .comb.rotor(2 => -1).map: { @_[0] ′ @_[1] } }</lang>
- Output:
./strange.raku | wc 87 87 348
REXX
<lang rexx>/*REXX pgm lists a bunch of strange numbers, integers whose digits differ by a prime. */ parse arg LO HI . /*obtain optional arguments from the CL*/ if LO== | LO=="," then LO= 100 /*Not specified? Then use the default.*/ if HI== | HI=="," then HI= 500 /* " " " " " " */ !.= 0; !.2= 1; !.3= 1; !.5= 1; !.7= 1 /*build a list of digits that are prime*/ $= /*the list of strange numbers (so far).*/
do j=LO to HI; L= length(j) /*find for strange numbers in the range*/ if L==1 then iterate /*Number too short? Then skip it. */ do k=1 for L-1 /*examine the difference in the digits.*/ dif= abs( substr(j, k, 1) - substr(j, k + 1, 1) ) /* │difference│ */ if \!.dif then iterate j /*Difference not prime? Then skip it. */ end /*k*/ $= $ j /*add the number to the "strange" list.*/ end /*j*/ /*stick a fork in it, we're all done. */
say words($) ' strange numbers found between ' LO " and " HI ' (inclusive):'; say say strip($)</lang>
- output when using the default inputs:
87 strange numbers found between 100 and 500 (inclusive): 130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 242 246 247 249 250 252 253 257 258 270 272 274 275 279 292 294 296 297 302 303 305 307 313 314 316 318 350 352 353 357 358 361 363 364 368 369 381 383 385 386 413 414 416 418 420 424 425 427 429 461 463 464 468 469 470 472 474 475 479 492 494 496 497
Ring
<lang ring> load "stdlib.ring"
row = 0 see "Strange 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 = fabs(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 numbers are: 131 135 136 138 141 142 146 147 149 161 164 168 169 181 183 185 186 202 203 205 241 242 246 247 249 250 252 253 257 258 272 274 275 279 292 294 296 297 302 303 307 313 314 316 318 350 352 353 357 358 363 364 368 369 381 383 385 386 413 414 418 420 424 425 427 429 461 463 464 468 470 472 474 475 479 492 494 496 497
Wren
<lang ecmascript>var primes = [2, 3, 5, 7] var count = 0 var d = [] System.print("Strange 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]).abs) && primes.contains((d[1] - d[2]).abs)) { System.write("%(i) ") count = count + 1 if (count % 10 == 0) System.print() }
} if (count % 10 != 0) System.print() System.print("\n%(count) strange numbers in all.")</lang>
- Output:
Strange numbers in the open interval (100, 500) are: 130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 242 246 247 249 250 252 253 257 258 270 272 274 275 279 292 294 296 297 302 303 305 307 313 314 316 318 350 352 353 357 358 361 363 364 368 369 381 383 385 386 413 414 416 418 420 424 425 427 429 461 463 464 468 469 470 472 474 475 479 492 494 496 497 87 strange numbers in all.