Largest proper divisor of n: Difference between revisions
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
Not a robot (talk | contribs) (Add FOCAL) |
||
Line 93: | Line 93: | ||
27 41 1 42 17 43 29 44 1 45 |
27 41 1 42 17 43 29 44 1 45 |
||
13 46 31 47 19 48 1 49 33 50</pre> |
13 46 31 47 19 48 1 49 33 50</pre> |
||
=={{header|FOCAL}}== |
|||
<lang focal>01.10 F N=1,100;D 2;D 3 |
|||
01.20 Q |
|||
02.10 S V=1 |
|||
02.20 I (1-N)2.3;R |
|||
02.30 S V=N-1 |
|||
02.40 S A=N/V |
|||
02.50 I (FITR(A)-A)2.6;R |
|||
02.60 S V=V-1 |
|||
02.70 G 2.4 |
|||
03.10 T %2,V |
|||
03.20 S A=N/10 |
|||
03.30 I (FITR(A)-A)3.4;T ! |
|||
03.40 R</lang> |
|||
{{out}} |
|||
<pre>= 1= 1= 1= 2= 1= 3= 1= 4= 3= 5 |
|||
= 1= 6= 1= 7= 5= 8= 1= 9= 1= 10 |
|||
= 7= 11= 1= 12= 5= 13= 9= 14= 1= 15 |
|||
= 1= 16= 11= 17= 7= 18= 1= 19= 13= 20 |
|||
= 1= 21= 1= 22= 15= 23= 1= 24= 7= 25 |
|||
= 17= 26= 1= 27= 11= 28= 19= 29= 1= 30 |
|||
= 1= 31= 21= 32= 13= 33= 1= 34= 23= 35 |
|||
= 1= 36= 1= 37= 25= 38= 11= 39= 1= 40 |
|||
= 27= 41= 1= 42= 17= 43= 29= 44= 1= 45 |
|||
= 13= 46= 31= 47= 19= 48= 1= 49= 33= 50</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
Revision as of 16:07, 1 June 2021
- Task
- a(1) = 1; for n > 1, a(n) = largest proper divisor of n, where n < 101 .
APL
<lang apl>(⌈/1,(⍸0=¯1↓⍳|⊢))¨10 10⍴⍳100</lang>
- Output:
1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50
BASIC
<lang basic>10 DEFINT A-Z 20 FOR I=1 TO 100 30 IF I=1 THEN PRINT " 1";: GOTO 70 40 FOR J=I-1 TO 1 STEP -1 50 IF I MOD J=0 THEN PRINT USING "###";J;: GOTO 70 60 NEXT J 70 IF I MOD 10=0 THEN PRINT 80 NEXT I</lang>
- Output:
1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50
BCPL
<lang bcpl>get "libhdr"
let lpd(n) = valof
for i = n<=1 -> 1, n-1 to 1 by -1 if n rem i=0 resultis i
let start() be
for i=1 to 100 $( writed(lpd(i), 3) if i rem 10=0 then wrch('*N') $)</lang>
- Output:
1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50
C
<lang c>#include <stdio.h>
unsigned int lpd(unsigned int n) {
if (n<=1) return 1; int i; for (i=n-1; i>0; i--) if (n%i == 0) return i;
}
int main() {
int i; for (i=1; i<=100; i++) { printf("%3d", lpd(i)); if (i % 10 == 0) printf("\n"); } return 0;
}</lang>
- Output:
1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50
FOCAL
<lang focal>01.10 F N=1,100;D 2;D 3 01.20 Q
02.10 S V=1 02.20 I (1-N)2.3;R 02.30 S V=N-1 02.40 S A=N/V 02.50 I (FITR(A)-A)2.6;R 02.60 S V=V-1 02.70 G 2.4
03.10 T %2,V 03.20 S A=N/10 03.30 I (FITR(A)-A)3.4;T ! 03.40 R</lang>
- Output:
= 1= 1= 1= 2= 1= 3= 1= 4= 3= 5 = 1= 6= 1= 7= 5= 8= 1= 9= 1= 10 = 7= 11= 1= 12= 5= 13= 9= 14= 1= 15 = 1= 16= 11= 17= 7= 18= 1= 19= 13= 20 = 1= 21= 1= 22= 15= 23= 1= 24= 7= 25 = 17= 26= 1= 27= 11= 28= 19= 29= 1= 30 = 1= 31= 21= 32= 13= 33= 1= 34= 23= 35 = 1= 36= 1= 37= 25= 38= 11= 39= 1= 40 = 27= 41= 1= 42= 17= 43= 29= 44= 1= 45 = 13= 46= 31= 47= 19= 48= 1= 49= 33= 50
Haskell
<lang haskell>import Data.List.Split (chunksOf) import Text.Printf (printf)
lpd :: Int -> Int lpd 1 = 1 lpd n = head $ [x | x <- [n-1, n-2 .. 1], n `mod` x == 0]
main :: IO () main = putStr $
unlines $ map concat $ chunksOf 10 $ map (printf "%3d") $ map lpd [1..100]</lang>
- Output:
1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50
Raku
A little odd to special case a(1) == 1 as technically, 1 doesn't have any proper divisors... but it matches OEIS A032742 so whatever. <lang perl6>use Prime::Factor;
say (flat 1, (2..100).map: *.&proper-divisors.sort.tail ).batch(10)».fmt("%2d").join: "\n";</lang>
- Output:
1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50
Ring
<lang ring> see "working..." + nl see "Largest proper divisor of n are:" + nl see "1 " row = 1 limit = 100
for n = 2 to limit
for m = 1 to n-1 if n%m = 0 div = m ok next row = row + 1 see "" + div + " " if row%10 = 0 see nl ok
next
see "done..." + nl </lang>
- Output:
working... Largest proper divisor of n are: 1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50 done...
Wren
<lang ecmascript>import "/math" for Int import "/fmt" for Fmt
System.print("The largest proper divisors for numbers in the interval [1, 100] are:") System.write(" 1 ") for (n in 2..100) {
if (n % 2 == 0) { Fmt.write("$2d ", n / 2) } else { Fmt.write("$2d ", Int.properDivisors(n)[-1]) } if (n % 10 == 0) System.print()
}</lang>
- Output:
The largest proper divisors for numbers in the interval [1, 100] are: 1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50