Help me solve this please :(

Hey can someone help me so solve this please in c++ ? :(

Given natural numbers m and n.
Find all numbers in the interval
[n, m], which coincide with the last of their squares
numbers (e.g., 6 for square 36, 25 for square 625).

Digits, if any, must be numerically separated.
To use a function that returns a number consisting of,
the last k digits of another number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cctype>
#include <limits>

int main()
{
   std::cout << "Do you want someone to do all the work for you? ";
   char answer{};
   std::cin >> answer;

   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   if (std::toupper(answer) == 'Y' ) { std::cout << "Post it in the Jobs section.\n"; }

   else { std::cout << "Show what you have coded so far.\n"; }

   std::cout << "Good luck.\n";
}
i did not wanted to embarrass myself but i have got this far and i dont think so its even correct i cant figure out algorithm :(

#include <iostream>
using namespace std;

int result (int n, int m)
{
int k=0;
for (i=1;i>=n; i<=m i++)
k=(i * i)/10;
return k;
}



int main (){
int ok;
int m;
int n;
int k;
do {
cout <<"enter n:"<< endl;
cin >>n;
cout << "enter m:"<< endl;
cin >> m;
cout <<"Result:"result (k)<<endl;

cout<<"to continue (1) or end (0)?"<< endl;
cin>>ok;
}while (ok==1)
return 0;
}
PLEASE learn to use code tags, it makes reading and commenting on your source code MUCH easier.

Hint: you can edit your post and add code tags.

http://www.cplusplus.com/articles/jEywvCM9/

I had to do a lot of rewriting to get this to compile.
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
#include <iostream>

int result(int n, int m)
{
   int k { };
   for (int i = n; i <= m; i++)
   {
      k = (i * i) / 10;
   }

   return k;
}

int main()
{
   char ok { };

   do
   {
      std::cout << "enter n: ";
      int n;
      std::cin >> n;

      std::cout << "enter m: ";
      int m;
      std::cin >> m;
      std::cout << "Result: " << result(n, m) << '\n';

      std::cout << "\nDo you want to go again? (y or n)? ";
      std::cin >> ok;
   }
   while (ok == 'y');
}

enter n: 4
enter m: 15
Result: 22

Do you want to go again? (y or n)? n

What the algorithm is supposed to be, I don't have a clue. Apparently the output will have multiple numbers.

If I read the requirements correctly.
im sorry i will keep this in mind ^^i think i can use some tips for this, thank you ! but as i got it right the point of it is to that user enters 2 numbers n and m what is the interval. for example

n:2
m:8
we get number interval: 2;3;4;5;6;7;8
and we need to find from these numbers which are equal to the last of its square
numbers.

for example 2^2=4 (not equal)
3^2=6 (not equal)
4^2=12 ( 12 last digit 2 is not equal to 4)
5^2=25 (here number 25 last digit (5) is equal to our 5)
6^2=36 (here number 36 last digit (6) is equal to our 6 at start)
etc
i was thinking k=(i * i)/10; it could get us square and then show last number, but it think it cant work on bigger numbers like 25^2=625 (where our 25 is equal was 25 from 625.

in the end from this interval program need to show numbers 5 and 6.

i tried to explain as good as i could.
you are working way too hard.

int lt[] = {0,1,4,9,6,5,6,9,4,1};
answer = lt[input%10];

or more precisely for your specific question,
if(input%10 == lt[input%10])
then its what you were looking for.

once you loop *that* one, you see it can be reduced to even easier:
bool lt2[] = {1,1,0,0,0,1,1,0,0,0}
answer = lt2[input%10];

as it turns out that inputs that end in 0, 1, 5, and 6 are the numbers you seek, and everything else is not matched, eg:
if( lt2[input%10])
//its one of your values.


the lesson here is to check out the problem before you try to code it. I got all that just by looking for a pattern, by doing a for loop that printed the last digit of numbers vs the last digit of the square of the numbers. In 2 min I solve your problem in 2 lines of code + 5 lines of throwaway code for my look. Do you agree that this gives the answer?
Last edited on
jonnin wrote:
Do you agree that this gives the answer?
No.


Possible solutions up to 17 digits:
0
1
5
6
25
76
376
625
9376
90625
109376
890625
2890625
7109376
12890625
87109376
212890625
787109376
1787109376
8212890625
18212890625
81787109376
918212890625
9918212890625
40081787109376
59918212890625
259918212890625
740081787109376
3740081787109376
6259918212890625
43740081787109376
56259918212890625



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
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <set>
using namespace std;

using INT = unsigned long long;

int main()
{
   const int MAXDIGITS = 17;

   set<INT> S = { 0, 1 };

   INT twodm1 = 1, twod = 2, fived = 5;
   for ( int d = 1; d <= MAXDIGITS; d++ )
   {
      INT y1 = twodm1 / 5;
      if ( y1 % 2 == 0 ) y1++;

      // Odd solution with d digits
      for ( INT y = y1; y < twod; y += 2 )
      {
         INT n = fived * y;
         if ( (n - 1) % twod == 0 )
         {
            S.insert( n );
            break;
         }
      }

      // Even solution with d digits
      for ( INT y = y1; y < twod; y += 2 )
      {
         INT n = fived * y + 1;
         if ( n % twod == 0 )
         {
            S.insert( n );
            break;
         }
      }

      twodm1 = twod;   twod *= 2;   fived *= 5;
   }

   // Output
   for ( INT n : S ) cout << n << '\n';
}



Brute force for smaller number of digits (takes too long on cpp.sh for 9 digits; will overflow for more than 9 digits).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <set>
using namespace std;

using INT = unsigned long long;

int main()
{
   const int MAXDIGITS = 8;
   set<INT> S = { 0, 1 };

   for ( INT d = 1, divisor = 10; d <= MAXDIGITS; d++, divisor *= 10 )
   {
      INT n1 = divisor / 10, n2 = divisor - 1;
      for ( INT n = n1; n <= n2; n++ )
      {
         if ( (n * n - n) % divisor == 0 ) S.insert( n );
      }
   }

   for ( INT n : S ) cout << n << '\n';
}
Last edited on
so not 11*11 = 121 ? I don't see it.
nevermind. not the same numbers as the examples.
but, all the answers to this one appear to also be in my filter... and maybe only 5s and 6s after a while?
Last edited on
@jonnin, the WHOLE of n appears at the back of n^2, not just the last digit. 11 is not the last two digits of 121.
If we have a positive integer n, log10n + 1 provides the number of its digits.
The modulo (or remainder) division by powers of 10 provides the rightmost digits of an integer, i.e. 25 mod 10 = 5 and 625 mod 100 = 25.
So the trick should be:
if n is equal to n2 mod ( 10log10n + 1 ) ...

Example output:
              5 -->                   25
              6 -->                   36
             25 -->                  625
             76 -->                 5776
            376 -->               141376
            625 -->               390625
           9376 -->             87909376
          90625 -->           8212890625
         109376 -->          11963109376
         890625 -->         793212890625
        2890625 -->        8355712890625
        7109376 -->       50543227109376
       12890625 -->      166168212890625
       87109376 -->     7588043387109376
      212890625 -->    45322418212890625
      787109376 -->   619541169787109376
That took 434 seconds.

Enoizat wrote:
That took 434 seconds.

How much???

Try the first code here:
http://www.cplusplus.com/forum/beginner/264349/#msg1139460


Last edited on
lastchance wrote:
Try the first code here:

Awesome code, @lastchance.
Registered users can post here. Sign in or register to post.