[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

need help with a coding homework

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 14
Thread images: 1

File: C Program.png (4KB, 442x85px)
C Program.png
4KB, 442x85px
thanks beforehand
sorry if it's not clear
>>
>>310708
int sum = 0;
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
sum += pow(3, i+j);

do some casting
>>
>>310708
∑∑3^i+j = ∑3^i * ∑3^j = ∑3^i (3^(n+1) - 1)/2 = (3^(n+1) - 1)^2 /4

unsigned long long ans_direct(unsigned n){
unsigned long long result = 3;
unsigned long long pow = 3;
while(n){ // compute 3^n+1 by repeated squaring
if(n&1){
result *= pow;
}
pow*=pow;
n>>=1;
}

result--; //3^n+1 - 1
result>>=1; (3^n+1 - 1)/2
result*=result; //(3^n+1 - 1)^2 /4

return result;
}

unsigned long long ans_sum(unsigned n){
unsigned long long result=0;
unsigned long long three_i=1;
unsigned long long three_i_j=1;
for(unsigned i=0; i<=n; i++){
three_i_j=three_i;
for(unsigned j=0; j<=n; j++){
result += three_i_j;
three_i_j*=3;
}
three_i*=3;
}
return result;
}

Protip: n>19 will overflow. since log_2(3) * 21 * 2 - 2 = 64.6~ bits
>>
Sum is equal to ((3^(n+1)-1)/2)^2
Translate that into C, the other algorithms posted here are naive brute force and not efficient
>>
>>311342
good way of starting an argument with the instructor
>nice but this is not a maths course
>but programming is maths
etc...
>>
>>311342
>other algorithms posted here are naive brute force and not efficient

Did you miss the post right above you?
>>
>>311342
What's the method for solving these? Curious
>>
>>312088
(x-1)(1+x+x^2+...+x^n) = x^(n+1) - 1
1+x+x^2+...+x^n = (x^(n+1) - 1) / (x-1)
>>
>>311347
It's the computer that's supposed to be doing the math, not you. So if you want to implement what you did to simplify that and make a tiny MATLAB, then go right ahead.

>>311752
Formatting is important. That post is a lot of effort to decipher, and I imagine no-one bothered. I hope OP didn't, because he's gonna get marked for implementing the algorithm as described.
>>
>>312088
It's just a geometric sum. It's the same principle as figuring out 0.333...=1/3.

Take the sum S=∑3^j [from j=0 to n] and multiple it by its ratio between sequential terms
3^(j+1)/3^j = 3
3S = ∑3^(j+1) [from j=0 to n] = ∑3^j [from j=1 to n+1]

Now subtract the original sum and notice that nearly all the terms cancel out.

3S-S = ∑3^j [from j=1 to n+1] - ∑3^j [from j=0 to n] = ∑3^j [j = n+1 to n+1] + ∑(3^j - 3^j) [from j=1 to n] - ∑3^j [from j=0 to 0] = 3^(n+1) - 3^0

Solve for S:
3^(n+1) - 3^0 = 3S-S = (3-1)*S
S= (3^(n+1) - 3^0) / (3-1)

In general, for a sum of ratio to a power ∑ratio^j [from j=k to n] is equal to (ratio^(n+1) - ratio^k)(ratio - 1). If the ratio is less than 1 then n can become infinite.

ex:
∑3((1/10)^j) [from j=1 to infinity] = 3∑(1/10)^j [from j=1 to infinity] = 3( (1/10)^inf - 1/10^1 ) / (1/10 - 1) = 3( -1/10 ) / (-9/10) = 3*(1/9) = 1/3
>>
>>312099
>Formatting is important

It's not my fault there are no code or math tags on /wsr/

unsigned long long ans_direct(unsigned n){
░░unsigned long long result = 3;
░░unsigned long long pow = 3;
░░while(n){ // compute 3^n+1 by repeated squaring
░░░░if(n&1){
░░░░░░result *= pow;
░░░░}
░░░░pow*=pow;
░░░░n>>=1;
░░}
░░result--; //3^n+1 - 1
░░result>>=1; (3^n+1 - 1)/2
░░result*=result; //(3^n+1 - 1)^2 /4
░░return result;
}

unsigned long long ans_sum(unsigned n){
░░unsigned long long result=0;
░░unsigned long long three_i=1;
░░unsigned long long three_i_j=1;
░░for(unsigned i=0; i<=n; i++){
░░░░three_i_j=three_i;
░░░░for(unsigned j=0; j<=n; j++){
░░░░░░result += three_i_j;
░░░░░░three_i_j*=3;
░░░░}
░░░░three_i*=3;
░░}
░░return result;
}
>>
>>312296
instead of using long long you should error out for large n values
>>
>>312301
>effort
>>
>>312370
>unsigned long long
Thread posts: 14
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.