Reputation: 48986
I have the following formula:
F = X / 1+4+9+16+....+n^2
How can I write a program in QBasic
that find the result of F
?
Thanks.
Upvotes: 2
Views: 205
Reputation: 21
CLS
INPUT "Input the value of n: ", n%
INPUT "The value of X: ", X
denominator% = 0
FOR i% = 1 TO n%
denominator% = denominator% + i% ^ 2
NEXT i%
F = X / denominator%
PRINT "F = "; F
Upvotes: 2
Reputation: 1165
A power multiplier:
DEFDBL A-Z
INPUT "Input the value of n: ", n
INPUT "The value of X: ", X
INPUT "The power: ", p
denominator = 0
FOR i = 1 TO n
denominator = denominator + i ^ p
NEXT
F = X / denominator
PRINT "F = "; F
END
Upvotes: 1
Reputation: 213200
From this useful page, the sum of the squares of the first n natural numbers is:
So you just need to calculate:
F = X * 6 / (n * (n + 1) * (2 * n + 1))
Upvotes: 2