-
+ 6BD00B7EB4A16F1B9AC47526C946BDC9B6E130A18477DC0618A2484EC20B371EC72541F4984C2D43F8758EDC091C3AC083A85ADB195B9E949DD5D159192CF3E9
ffa/demos/primorial.peh
(0 . 0)(1 . 98)
386 (----------------------------------------------------------------------------)
387 (----------------------------------------------------------------------------)
388 (- Demo Tape for 'Peh'; produces the largest primorial that fits in Width. -)
389 (- -)
390 (- (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) -)
391 (- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html -)
392 (- -)
393 (- You do not have, nor can you ever acquire the right to use, copy or -)
394 (- distribute this software ; Should you use this software for any purpose, -)
395 (- or copy and distribute it to anyone or in any manner, you are breaking -)
396 (- the laws of whatever soi-disant jurisdiction, and you promise to -)
397 (- continue doing so for the indefinite future. In any case, please -)
398 (- always : read and understand any software ; verify any PGP signatures -)
399 (- that you use - for any purpose. -)
400 (- -)
401 (- See also http://trilema.com/2015/a-new-software-licensing-paradigm . -)
402 (----------------------------------------------------------------------------)
403 (----------------------------------------------------------------------------)
404
405 (------------------------------ Main Program : ------------------------------)
406
407 ( p is the 'primorial accumulator', and q is the current 'potential prime'. )
408
409 ( p is initialized to the product of the first two primes, 2 and 3 : )
410 .6 $p
411
412 ( q is initialized to 5, i.e. the first prime that is not 2 or 3 :)
413 .5 $q
414
415 ( Begin a loop: )
416 :
417
418 ( Determine GCD(p, q) : )
419 p q G
420
421 ( If GCD(p, q) WAS equal to 1, we know that q is a new prime : )
422 .1 =
423 {
424 ( Find the product pq.
425 The UPPER FZ of this product will land on top of stack,
426 and the LOWER FZ will lie immediately under it : )
427 p q *
428
429 ( If the UPPER FZ of the product pq was NOT equal to 0...
430 ... then we have overflowed our Width, and must stop: )
431 {
432 ( Drop the LOWER FZ of the product pq, because
433 we have overflowed Width and cannot use it : )
434 _
435
436 ( Leave a 0 on the stack, to trigger termination : )
437 .0
438
439 ( At this point, we have the largest primorial
440 that can fit in our FZ Width, and we are done. )
441 }
442
443 ( If the UPPER FZ of the product pq WAS equal to 0...
444 ... then we have NOT overflowed our Width, and continue: )
445 {
446 ( Store the LOWER FZ of the product pq to p :)
447 $p
448
449 ( Leave a 1 on the stack, to trigger continuation : )
450 .1
451
452 ( At this point, pq is the primorial up to and
453 inclusive of q, and we keep going. )
454 }_
455 }
456
457 ( If GCD(p, q) WAS NOT equal to 1, we know that q is NOT a prime : )
458 {
459 ( Leave a 1 on the stack, to signal continuation : )
460 .1
461 }_
462
463
464 ( After either of the above cases, we must:
465 q := q + 2,
466 given as any possible next prime after the current q must be odd : )
467 q .2 + $q
468
469 ( If we have a 1, cycle the loop; otherwise, we have the Primorial, in p,
470 and must output it and terminate the Tape : )
471 ,
472
473 ( Emit a Peh Tape which defines the constant 'Primorial' : )
474 [@Primorial@ ( Regs : none )
475 .]
476 p#
477 [;
478 ]
479
480 ( Now, terminate with a 'Yes' Verdict, as we have succeeded : )
481 QY
482
483 (--------------------------------~~The End~~---------------------------------)