Special odd numbers: Difference between revisions
Added C# entry, cleaned up Ring entry (it wasn't producing all 194 results)
(Added Wren) |
(Added C# entry, cleaned up Ring entry (it wasn't producing all 194 results)) |
||
Line 6:
<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}}==
Line 140 ⟶ 171:
=={{header|Ring}}==
limit = 1000
Prim = []▼
# table of prime numbers from 3 to 1000/3
pr = [ 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
▲see "Special odd numbers are:" + nl
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
if pr[nlim] * pr[nlim] > limit
▲Prim = []
exit▼
next
nlim--
# add items to result list and sort
▲for n = 1 to limit1
for m = n
▲ ok
ok
add(Prim, amt)
next
next
Prim = sort(Prim)▼
# display results
▲Prim = sort(Prim)
for n = 1 to len(Prim)
if Prim[n]
▲ exit
ok
see " " + Prim[n] + " "
if n %
see nl
ok
next
n--
{{out}}
<pre>working...
Special odd numbers are:
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
done...</pre>▼
▲Found 151 Special odd numbers.
▲done...
=={{header|Wren}}==
|