Abundant odd numbers: Difference between revisions

No edit summary
Line 2,864:
Dim sumaDiv As Integer = 0
 
Function SumaDivisores(n As Integer) As Integer=={{header|Fortran}}==
A basic direct solution. A more robust alternative would be to find
the prime factors and then use a formulaic approach.
<lang fortran>
program main
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer,parameter :: dp=kind(0.0d0)
character(len=*),parameter :: g='(*(g0,1x))'
integer :: j, icount
integer,allocatable :: list(:)
real(kind=dp) :: tally
 
write(*,*)'N sum'
icount=0 ! number of abundant odd numbers found
do j=1,huge(0)-1,2 ! loop through odd numbers for candidates
list=divisors(j) ! git list of divisors for current value
tally= sum([real(list,kind=dp)]) ! sum divisors
if(tally>2*j .and. iand(j,1) /= 0) then ! count an abundant odd number
icount=icount+1
select case(icount) ! if one of the values targeted print it
case(1:25,1000);write(*,g)icount,':',j!, list
end select
endif
if(icount.gt.1000)exit ! quit after last targeted value is found
enddo
 
do j=1000000001,huge(0),2
list=divisors(j)
tally= sum([real(list,kind=dp)])
if(tally>2*j .and. iand(j,1) /= 0) then
write(*,g)'First abundant odd number greater than one billion:',j
 
exit
endif
enddo
 
contains
 
function divisors(num) result (numbers)
!> brute force divisors
integer,intent(in) :: num
integer :: i
integer,allocatable :: numbers(:)
numbers=[integer :: ]
do i=1 , int(sqrt(real(num)))
if (mod(num , i) .eq. 0) numbers=[numbers, i,num/i]
enddo
end function divisors
 
end program main
</lang>
 
{{out}}
<pre>
N sum
1 : 945
2 : 1575
3 : 2205
4 : 2835
5 : 3465
6 : 4095
7 : 4725
8 : 5355
9 : 5775
10 : 5985
11 : 6435
12 : 6615
13 : 6825
14 : 7245
15 : 7425
16 : 7875
17 : 8085
18 : 8415
19 : 8505
20 : 8925
21 : 9135
22 : 9555
23 : 9765
24 : 10395
25 : 11025
1000 : 492975
First abundant odd number greater than one billion: 1000000575
</pre>
 
' Devuelve la suma de los divisores propios de n
Dim suma As Integer = 1
Anonymous user