fork(1) download
  1. $edge = sqrt 2 * 10 ** 9;
  2.  
  3. @_ = (1) x $edge;
  4.  
  5. for $i (2..$edge){
  6. $j = $i ** 2 + $i; # not excludes (prime ^ 2)s
  7. while ($j < $edge){
  8. $_[$j] = 0;
  9. $j += $i;
  10. }
  11. }
  12.  
  13. @primes = ();
  14. $_[$_-1] and push @primes, $_-1 for 1..@_;
  15. shift @primes; shift @primes;
  16. # made array of ~primes (2, 3, [4], 5, 7, [9], 11, 13, 17..)
  17.  
  18. @ans = ();
  19. for (1..<>){
  20. $_ = <>, chomp;
  21. $false = 1;
  22. for $i (@primes){
  23. $g = $i; # copy of $i (~prime)
  24. $i > $_ and last;
  25. ($_ + ($i % 2 ? 0 : $i / 2 )) % $i or ($false-- and last);
  26. }
  27. if ($_ / $g - $g / 2 + 1 < 1){ $false = 1}
  28. push @ans,( $false ? IMPOSSIBLE : ($_, " = ", join ' + ', ($_ / $g - $g / 2 + 1 .. $_ / $g + $g / 2)));
  29. push @ans, "\n";
  30. }
  31. print @ans
Success #stdin #stdout 0.08s 5000KB
stdin
30 - number of lines will try to solve:
8
10
24
1
2
3
4
5
123
1234
12345
123456
1234567
12345678
123456789
1000000000
998812807
143
286
572
1144
2288
4576
44
55
72
stdout
IMPOSSIBLE
10 = 1 + 2 + 3 + 4
24 = 7 + 8 + 9
IMPOSSIBLE
IMPOSSIBLE
3 = 1 + 2
IMPOSSIBLE
5 = 2 + 3
123 = 61 + 62
1234 = 307 + 308 + 309 + 310
12345 = 6172 + 6173
123456 = 41151 + 41152 + 41153
1234567 = 617283 + 617284
12345678 = 4115225 + 4115226 + 4115227
123456789 = 61728394 + 61728395
1000000000 = 199999998 + 199999999 + 200000000 + 200000001 + 200000002
998812807 = 499406403 + 499406404
143 = 71 + 72
286 = 70 + 71 + 72 + 73
572 = 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57
1144 = 99 + 100 + 101 + 102 + 103 + 104 + 105 + 106 + 107 + 108 + 109
2288 = 203 + 204 + 205 + 206 + 207 + 208 + 209 + 210 + 211 + 212 + 213
4576 = 411 + 412 + 413 + 414 + 415 + 416 + 417 + 418 + 419 + 420 + 421
IMPOSSIBLE
55 = 27 + 28
72 = 23 + 24 + 25
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE