Please Help Me To Do This !!

Hello Every One ☺


Please Help Me To Do This ( 64 times )
I Want Only Using ( For Loop And Arrays )
Note : I don't Want Solutions Mathematics I Know It's ( 2 * 3^62 )
♥♥♥

https://i.postimg.cc/qqcpd7WJ/Capture.png
Last edited on
This shouldn't be in the windows forum and please be more specific about exactly what you want to do.
Last edited on
you can store each term in an array and double for loop, the inner one adds up the array so far, the outer one is for N terms.
From what I see, this is what you are trying to do 64 times:

1
2
3
4
5
6
int  a=1;
int  b=a*2;
int  c=(a+b)*2;
int  d=(a+b+c)*2;
int  e=(a+b+c+d)*2;
...etc...


Now, if you look closely at your pattern, you will notice from 'c' onward, that a=b/2, (a+b)=c/2, (a+b+c)=d/2, etc. So, using simple algebra, this can be simplified to:

1
2
3
4
5
6
int  a=1;
int  b=a*2;
int  c=(b/2+b)*2;
int  d=(c/2+c)*2;
int  e=(d/2+d)*2;
...etc...


Notice that expressions like "c=(b/2+b)*2" can be simplified to "c=(3*b/2)*2", and further simplified to "c=b*3". Now, replace "a, b, c, etc..." with an array, so it can be iterated 64 times, "A[64]". So, that code becomes:

1
2
3
4
5
6
7
8
int main(){
     int i, A[64];
     
     A[0]=1;
     A[1]=A[0]*2;
     for(i=2; i<64; i++)  A[i]=A[i-1]*3;

     cout <<  A[63];


Hope this helped.
I do need to note, that this program likely won't run. Even if you were to use the highest possible precision for your variables (unsigned long long int), you can still only reach values of 2^64. Your solution would be much more than that.

Perhaps, someone here has a solution to this limitation?


1
2
3
4
5
6
7
8
    int main(){
     int i, A[64];
     
     A[0]=1;
     A[1]=A[0]*2;
     for(i=2; i<64; i++)  A[i]=A[i-1]*3;

     cout <<  A[63]; } 




@anachronon


Thank You So So Much Bro This is Exactly What I want ♥♥♥

Please Last Question How I Can Solve same This Problem In future ?? Should I learning Algorithm Or What I Should Learning ??

Last edited on
@mostakim hamza
Your code produces the wrong answer.

Take note what @anachronon wrote:
I do need to note, that this program likely won't run. Even if you were to use the highest possible precision for your variables (unsigned long long int), you can still only reach values of 2^64. Your solution would be much more than that.

Perhaps, someone here has a solution to this limitation?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string operator * ( const string &digits, int n )
{
   vector<int> V( digits.size(), 0 );    // Stores reversed number
   for ( size_t i = 0; i < digits.size(); i++ ) V[i] = ( digits[digits.size() - 1 - i] - '0' ) * n;

   // Sort out carry operation
   for ( size_t i = 0; i < V.size(); i++ )
   {
      if ( V[i] >= 10 )
      {
         int carry = V[i] / 10;
         V[i] %= 10;
         if ( i == V.size() - 1 ) V.push_back( carry );
         else                     V[i+1] += carry;
      }
   }

   string result( V.size(), ' ' );
   for ( size_t i = 0; i < V.size(); i++ ) result[V.size()-1-i] = '0' + V[i];
   return result;
}


int main()
{
   const int N = 64;
   string a[N];
   a[0] = "1";   a[1] = "2";
   for ( int i = 2; i < N; i++ ) a[i] = a[i-1] * 3;
   for ( int i = 0; i < N; i++ ) cout << i << '\t' << a[i] << '\n';
}



You can check it with python (which allows integers of arbitrary size):
print( 2 * 3**62 )
763040848953891663257299797618


Last edited on

Please Last Question How I Can Solve same This Problem In future ?? Should I learning Algorithm Or What I Should Learning ??


Really, my derivation was just basic Algebra.
Really, my derivation was just basic Algebra.
which is why I said double loop: I thought he did NOT want it condensed and wanted explicit computations for some odd reason.
One alternative is to use compiler extensions. The challenge just becomes printing the number itself, since the standard library doesn't support it :P
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Example program
#include <iostream>
using uint128 = unsigned __int128; // GCC extension

void print_uint128_helper(std::ostream& os, uint128 n)
{
    if (n == 0)
        return;

    print_uint128_helper(os, n/10);
    os << static_cast<int>(n % 10);
}

std::ostream& operator<<(std::ostream& os, uint128 n)
{
    if (n == 0)
        return os << 0;

    // there probably is some better built-in extension
    print_uint128_helper(os, n);
    return os;
}

int main()
{ 
    uint128 A[64];
    
    A[0] = 1;
    A[1] = 2;
    for (int i = 2; i < 64; i++)
        A[i] = A[i-1] * 3;
    
    for (int i = 0; i < 64; i++)
        std::cout << i << '\t' << A[i] << '\n';   
}
Last edited on
@lastchance

Your code produces the wrong answer.

Take note what @anachronon wrote


Yeh Brother You are Programming professional Please Can You Tell Me How I can be Like You Does 😥 Because I learning C++ In Here " https://www.w3schools.com/cpp/cpp_arrays.asp " And Almost finished But I Can't Do Same You Do 😥

If I read All This Tutorials ==> " http://www.cplusplus.com/doc/tutorial/ " Is It Enough Or Not ?? I don't want fall in the same wrong again


@anachronon
Really, my derivation was just basic Algebra.

Ok brother It Is basic Algebra Thank You So Much ❤️❤️❤️
Last edited on
Registered users can post here. Sign in or register to post.