Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding "perfect" numbers
#1
i was trying to write a code for finding perfect numbers but I cannot seem to get it working. any ideas?

Function find_perfect_numbers
Code:
Copy      Help
int N D T F
F=0
N=28
D=2
T=0
;out D
;Start
if F=1
,end

if D%N=0
/////////////////////////
,T+D;;out D
/////////////////////////
if D=N
,F=1
,if N=T
,,out "yes"
,else
,,out "no"

;out 1
D+1
goto Start
#2
Function find_perfect_numbers
Code:
Copy      Help
int N D T F
F=0
N=26
D=1
T=0
;Start
if F=1
,end

if D%N=0 ;; this seems to be setting D to whatever N is???
,T+D;;out D

if D=N
,F=1
,if N=T
,,out "yes"
,else
,,out "no"

out 1
D+1
goto Start
#3
this works for the first 5 but not sure how wise it is to use array for numbers larger than 4th perfect number (6,28,496,8128,33550336). best of luck to ya.
Function find_perfect_numbers2
Code:
Copy      Help
int sn cn ii fnc
sn=8128
ARRAY(int) pnc.create(sn)
cn=1
for ii cn sn
,if !(sn%cn)
,,pnc[ii]=cn
,,out pnc[ii]
,,fnc=fnc+pnc[ii]
,cn+1
fnc=fnc+sn/2
if fnc=sn
,out "perfect number"
else
,out "not pefect number"
#4
tweaked it a little array only populated now with the exact amount of elements it needs to hold divisor numbers.
Function find_perfect_numbers3
Code:
Copy      Help
int sn cn ii fnc i
sn=1500000
ARRAY(int) pnc
cn=1
for ii cn sn
,if !(sn%cn)
,,pnc[]=cn
,,out pnc[i]
,,fnc=fnc+pnc[i]
,,i+1
,cn+1
fnc=fnc+sn/2
if fnc=sn
,out "perfect number"
else
,out "not pefect number"

You May have to change array type as u get into larger numbers though.but this is not the best way to test either and is limited in how far it can go.
http://www.mersenne.org/ has software just for doing this
#5
Macro Perfect Numbers
Code:
Copy      Help
out

int n i s

for n 2 10000
,s=1
,int si=sqrt(n)+1
,for i 2 si 2
,,if !(n%i)
,,,s+i+(n/i)
,if i*i=n
,,s+i
,if n=s
,,out n

;6
;28
;496
;8128
#6
last post on this.
This will do the first 8 and would do more if had a large enough number range, but can only do the first 8
;Perfect Number = 2^(n-1) * (2^n - 1)
out
ARRAY(str) mpn
mpn="2[]3[]5[]7[]13[]17[]19[]31[]61[]89[]107[]127[]521[]607[]1279[]2203[]2281[]3217[]4253[]4423[]9689[]9941[]11213[]19937[]21701[]23209[]44497[]86243[]110503[]132049[]216091[]756839[]859433[]1257787[]1398269"
long n i x
for i 0 8;; number goes out of range after 8.
,n=val(mpn[i])
,x=pow(2 (n-1)) * (pow(2 n)-1)
,out x

;;output
;;6
;;28
;;496
;;8128
;;33550336
;;8589869056
;;137438691328
;;2305843008139952128

wont show the whole ARRAY here.The rest of it after 86243 is
[]110503[]132049[]216091[]756839[]859433[]1257787[]1398269"
but really only need up to 31 after that number is too big


Forum Jump:


Users browsing this thread: 2 Guest(s)