Increasing gaps between consecutive Niven numbers: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add MAD) |
Not a robot (talk | contribs) (Add Fortran) |
||
Line 375: | Line 375: | ||
27 153 179,838,772 2,998,895,823</pre> |
27 153 179,838,772 2,998,895,823</pre> |
||
=={{header|Fortran}}== |
|||
{{trans|C}} |
|||
<lang Fortran> program nivengaps |
|||
implicit none |
|||
integer*8 prev /1/, gap /0/, sum /0/ |
|||
integer*8 nividx /0/, niven /1/ |
|||
integer gapidx /1/ |
|||
character*13 idxfmt |
|||
character*14 nivfmt |
|||
write (*,*) 'Gap no Gap Niven index Niven number ' |
|||
write (*,*) '------ --- ------------- --------------' |
|||
10 call divsum(niven, sum) |
|||
if (mod(niven, sum) .EQ. 0) then |
|||
if (niven .GT. prev + gap) then |
|||
gap = niven - prev |
|||
call fmtint(nividx,13,idxfmt) |
|||
call fmtint(prev,14,nivfmt) |
|||
write (*,20) gapidx,gap,idxfmt,nivfmt |
|||
gapidx = gapidx + 1 |
|||
end if |
|||
prev = niven |
|||
nividx = nividx + 1 |
|||
end if |
|||
niven = niven + 1 |
|||
if (gapidx .LE. 32) go to 10 |
|||
stop |
|||
20 format (i7,' ',i3,' ',a13,' ',a14) |
|||
end program |
|||
C Sum of divisors of NN, given the sum of divisors of NN-1 |
|||
subroutine divsum(nn,sum) |
|||
implicit none |
|||
integer*8 n,nn,sum |
|||
n = nn |
|||
sum = sum + 1 |
|||
30 if (n.GT.0 .AND. mod(n,10).EQ.0) then |
|||
sum = sum - 9 |
|||
n = n / 10 |
|||
go to 30 |
|||
end if |
|||
end subroutine |
|||
integer*8 function mod(a,b) |
|||
implicit none |
|||
integer*8 a,b |
|||
mod = a - a/b * b |
|||
end function |
|||
C Format a positive integer with ',' as the thousands separator. |
|||
subroutine fmtint(num, len, str) |
|||
implicit none |
|||
integer*8 n, num |
|||
integer pos, len, th |
|||
character(*) str |
|||
n=num |
|||
pos=len |
|||
th=2 |
|||
40 if (pos.GT.0) then |
|||
if (n.EQ.0) then |
|||
str(pos:pos) = ' ' |
|||
else |
|||
str(pos:pos) = achar(mod(n,10) + iachar('0')) |
|||
if (th.EQ.0 .AND. n.GE.10 .AND. pos.GT.1) then |
|||
th = 2 |
|||
pos = pos-1 |
|||
str(pos:pos) = ',' |
|||
else |
|||
th = th-1 |
|||
end if |
|||
end if |
|||
pos = pos - 1 |
|||
n = n/10 |
|||
go to 40 |
|||
end if |
|||
end subroutine</lang> |
|||
{{out}} |
|||
<pre> Gap no Gap Niven index Niven number |
|||
------ --- ------------- -------------- |
|||
1 1 1 1 |
|||
2 2 10 10 |
|||
3 6 11 12 |
|||
4 7 26 63 |
|||
5 8 28 72 |
|||
6 10 32 90 |
|||
7 12 83 288 |
|||
8 14 102 378 |
|||
9 18 143 558 |
|||
10 23 561 2,889 |
|||
11 32 716 3,784 |
|||
12 36 1,118 6,480 |
|||
13 44 2,948 19,872 |
|||
14 45 4,194 28,971 |
|||
15 54 5,439 38,772 |
|||
16 60 33,494 297,864 |
|||
17 66 51,544 478,764 |
|||
18 72 61,588 589,860 |
|||
19 88 94,748 989,867 |
|||
20 90 265,336 2,879,865 |
|||
21 99 800,054 9,898,956 |
|||
22 108 3,750,017 49,989,744 |
|||
23 126 6,292,149 88,996,914 |
|||
24 135 44,194,186 689,988,915 |
|||
25 144 55,065,654 879,987,906 |
|||
26 150 61,074,615 989,888,823 |
|||
27 153 179,838,772 2,998,895,823 |
|||
28 192 399,977,785 6,998,899,824 |
|||
29 201 497,993,710 8,889,999,624 |
|||
30 234 502,602,764 8,988,988,866 |
|||
31 258 547,594,831 9,879,997,824 |
|||
32 276 1,039,028,518 18,879,988,824</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |