Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #58370 > unrolled thread

Rosetta: Count in factors

Started by"Robert L." <No_spamming@noWhere_7073.org>
First post2017-04-29 22:01 +0000
Last post2017-04-29 22:01 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.forth


Contents

  Rosetta: Count in factors "Robert L." <No_spamming@noWhere_7073.org> - 2017-04-29 22:01 +0000

#58370 — Rosetta: Count in factors

From"Robert L." <No_spamming@noWhere_7073.org>
Date2017-04-29 22:01 +0000
SubjectRosetta: Count in factors
Message-ID<oe32ge$cr3$1@gioia.aioe.org>
> Task
> 
> Write a program which counts up from 1, displaying each
> number as the multiplication of its prime factors.
> 
> For the purpose of this task, 1 (unity) may be shown as itself. 
> 
> 
> Example
> 
>    2   is prime,   so it would be shown as itself. 
>    6   is not prime;   it would be shown as   2 x 3
> 2144   is not prime;   it would be shown as   2 x 2 x 2 x 2 x 2 x 67


require 'prime'

puts " 1: 1" ;
(2..23).each{|n|
  printf "%2d: %s\n", n,
    Prime.prime_division(n).map{|f,c| ([f]*c).join ' x '}.join(' x ')
}

 1: 1
 2: 2
 3: 3
 4: 2 x 2
 5: 5
 6: 2 x 3
 7: 7
 8: 2 x 2 x 2
 9: 3 x 3
10: 2 x 5
11: 11
12: 2 x 2 x 3
13: 13
14: 2 x 7
15: 3 x 5
16: 2 x 2 x 2 x 2
17: 17
18: 2 x 3 x 3
19: 19
20: 2 x 2 x 5
21: 3 x 7
22: 2 x 11
23: 23

In Forth?


-- 
Amazon bans book after it received 300 5-star reviews.
http://www.tomatobubble.com/worldwarii.html

[toc] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web