I am trying to find the sum of integers ( (int)(a*i)/b ) where i is from 0 to 10^10.
The problem is a/b >=1 and can be a fraction also and a and b are constant
If it wasnt a fraction then it was really easy i*(i+1)/2 * a/b
Any ideas ?
AJ
I am trying to find the sum of integers ( (int)(a*i)/b ) where i is from 0 to 10^10.
The problem is a/b >=1 and can be a fraction also and a and b are constant
If it wasnt a fraction then it was really easy i*(i+1)/2 * a/b
Any ideas ?
AJ
Well, Sum to n terms is
[tex] \frac{n*(n+1)}{2} [/tex]
Where is the need for [tex]a/b[/tex] in your equation.
Please Be a little more elaborate so that we can understand what exactly is your aim.
see i need to find the sum of i terms such that
sum = summation ( (a*i)/b )where a,b constant and i moves from 0 - 10^10
if this was the only question then you could solve it as
(a/b)=constant
then constant*(i+1)*(i)/2
but what I have to find is (int)(ai/b)
now saw a = 3 and b =2; and i is 0 - 4
so answer would be
3*0/2 + 3*1/2 + 3*2/2+3*3/2+3*4/2
=
0 + (int)1.5 + (int)3 + (int)4.5 + (int)6
=
0 + 1 + 3 + 4 + 6
=
14
I hope this should be clear now
Hoping for some help
Regards
AJ
From what i have understood from your question, You want to know what would happen if [tex]a/b[/tex] is a fractional value and not an integer.
Consider the following.
[TEX]Given a=3, b=2;[/TEX]
consider a variable
[Tex]val = a/b;[/tex]
If we have [tex]val[/tex] to be a type int
it would probably store 1.
This is because the integer data-type just excludes the decimal places. That way we donot need to worry that
[Tex] val * (\frac{ i *(i+1)}{2}) [/tex] when i =4 would be equal to
[tex] 1 * ( \frac{4*5}{2})[/tex]
Which would be equal to .
[tex] 1 * (10) [/tex]
which is equal to 10.
Which is a faulty answer. but if you consider val to be of type double, float.
it would be
[tex]1.5 * 10 = 15[/tex]
Which would be faulty as well,
This problem i think would be best solved with the help of a for loop and iterating. Continously.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.