Special odd numbers: Difference between revisions
Content added Content deleted
(Added Wren) |
(Added C# entry, cleaned up Ring entry (it wasn't producing all 194 results)) |
||
Line 6: | Line 6: | ||
<br><br> |
<br><br> |
||
=={{header|C#|CSharp}}== |
|||
This reveals a set of non-prime numbers with exactly two factors for each '''<big>''n''</big>''', where '''<big>''1 < p < q < n''</big>'''. |
|||
<lang csharp>using System; using static System.Console; using System.Collections; |
|||
using System.Linq; using System.Collections.Generic; |
|||
class Program { static void Main(string[] args) { |
|||
int lmt = 1000, amt, c = 0, sr = (int)Math.Sqrt(lmt), lm2; var res = new List<int>(); |
|||
var pr = PG.Primes(lmt / 3 + 5).ToArray(); lm2 = pr.OrderBy(i => Math.Abs(sr - i)).First(); |
|||
lm2 = Array.IndexOf(pr, lm2); for (var p = 0; p < lm2; p++) { amt = 0; for (var q = p + 1; amt < lmt; q++) |
|||
res.Add(amt = pr[p] * pr[q]); } res.Sort(); foreach(var item in res.TakeWhile(x => x < lmt)) |
|||
Write("{0,4} {1}", item, ++c % 20 == 0 ? "\n" : ""); |
|||
Write("\n\nCounted {0} special odd prime products under {1}", c, lmt); } } |
|||
class PG { public static IEnumerable<int> Primes(int lim) { |
|||
var flags = new bool[lim + 1]; int j = 3; |
|||
for (int d = 8, sq = 9; sq <= lim; j += 2, sq += d += 8) |
|||
if (!flags[j]) { yield return j; |
|||
for (int k = sq, i = j << 1; k <= lim; k += i) flags[k] = true; } |
|||
for (; j <= lim; j += 2) if (!flags[j]) yield return j; } }</lang> |
|||
{{out}} |
|||
<pre> 15 21 33 35 39 51 55 57 65 69 77 85 87 91 93 95 111 115 119 123 |
|||
129 133 141 143 145 155 159 161 177 183 185 187 201 203 205 209 213 215 217 219 |
|||
221 235 237 247 249 253 259 265 267 287 291 295 299 301 303 305 309 319 321 323 |
|||
327 329 335 339 341 355 365 371 377 381 391 393 395 403 407 411 413 415 417 427 |
|||
437 445 447 451 453 469 471 473 481 485 489 493 497 501 505 511 515 517 519 527 |
|||
533 535 537 543 545 551 553 559 565 573 579 581 583 589 591 597 611 623 629 633 |
|||
635 649 655 667 669 671 679 681 685 687 689 695 697 699 703 707 713 717 721 723 |
|||
731 737 745 749 753 755 763 767 771 779 781 785 789 791 793 799 803 807 813 815 |
|||
817 831 835 843 849 851 865 869 871 879 889 893 895 899 901 905 913 917 921 923 |
|||
933 939 943 949 951 955 959 965 973 979 985 989 993 995 |
|||
Counted 194 special odd prime products under 1000</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 140: | Line 171: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
⚫ | |||
<lang ring> |
|||
load "stdlib.ring" |
|||
limit = 1000 |
|||
⚫ | |||
# table of prime numbers from 3 to 1000/3 |
|||
see "working..." + nl |
|||
pr = [ 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, |
|||
⚫ | |||
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, |
|||
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, |
|||
131, 137, 139, 149, 151, 157, 163, 167, 173, 179, |
|||
181, 191, 193, 197, 199, 211, 223, 227, 229, 233, |
|||
239, 241, 251, 257, 263, 269, 271, 277, 281, 283, |
|||
293, 307, 311, 313, 317, 331] |
|||
pl = len(pr) |
|||
# calculate upper limit for n |
|||
row = 0 |
|||
⚫ | |||
limit1 = 150 |
|||
if pr[nlim] * pr[nlim] > limit |
|||
⚫ | |||
⚫ | |||
⚫ | |||
next |
|||
nlim-- |
|||
# add items to result list and sort |
|||
⚫ | |||
for n = 1 to nlim |
|||
for m = n + 1 to pl |
|||
amt = pr[n] * pr[m] |
|||
if amt > limit |
|||
exit |
|||
⚫ | |||
ok |
ok |
||
add(Prim, amt) |
|||
next |
next |
||
next |
next |
||
⚫ | |||
# display results |
|||
⚫ | |||
for n = 1 to len(Prim) |
for n = 1 to len(Prim) |
||
if Prim[n] |
if Prim[n] < 100 |
||
see " " |
|||
⚫ | |||
ok |
ok |
||
see "" + Prim[n] + " " |
see " " + Prim[n] + " " |
||
if n% |
if n % 20 = 0 |
||
see nl |
see nl |
||
ok |
ok |
||
next |
next |
||
n-- |
|||
? nl + "Found " + n + " Special odd numbers." + nl + "done..."</lang> |
|||
see "done..." + nl |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre>working... |
||
working... |
|||
Special odd numbers are: |
Special odd numbers are: |
||
15 21 33 35 39 51 55 57 65 69 |
15 21 33 35 39 51 55 57 65 69 77 85 87 91 93 95 111 115 119 123 |
||
129 133 141 143 145 155 159 161 177 183 185 187 201 203 205 209 213 215 217 219 |
|||
77 85 87 91 93 95 111 115 119 123 |
|||
221 235 237 247 249 253 259 265 267 287 291 295 299 301 303 305 309 319 321 323 |
|||
129 133 141 143 145 155 159 161 177 183 |
|||
327 329 335 339 341 355 365 371 377 381 391 393 395 403 407 411 413 415 417 427 |
|||
185 187 201 203 205 209 213 215 217 219 |
|||
437 445 447 451 453 469 471 473 481 485 489 493 497 501 505 511 515 517 519 527 |
|||
221 235 237 247 249 253 259 265 267 287 |
|||
533 535 537 543 545 551 553 559 565 573 579 581 583 589 591 597 611 623 629 633 |
|||
291 295 299 301 303 305 309 319 321 323 |
|||
635 649 655 667 669 671 679 681 685 687 689 695 697 699 703 707 713 717 721 723 |
|||
327 329 335 339 341 355 365 371 377 381 |
|||
731 737 745 749 753 755 763 767 771 779 781 785 789 791 793 799 803 807 813 815 |
|||
391 393 395 403 407 411 413 415 417 427 |
|||
817 831 835 843 849 851 865 869 871 879 889 893 895 899 901 905 913 917 921 923 |
|||
437 445 447 451 469 473 481 485 493 497 |
|||
933 939 943 949 951 955 959 965 973 979 985 989 993 995 |
|||
505 511 515 517 527 533 535 545 551 553 |
|||
⚫ | |||
559 565 581 583 589 611 623 629 635 649 |
|||
⚫ | |||
655 667 671 679 685 689 695 697 703 707 |
|||
713 721 731 737 745 749 763 767 779 781 |
|||
791 793 799 803 817 851 869 871 889 893 |
|||
899 901 913 917 923 943 949 959 973 979 |
|||
989 |
|||
⚫ | |||
⚫ | |||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |