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.
#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;
}
#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');
}
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.
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?
#include <iostream>
#include <set>
usingnamespace std;
using INT = unsignedlonglong;
int main()
{
constint 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';
}
#include <iostream>
#include <set>
usingnamespace std;
using INT = unsignedlonglong;
int main()
{
constint 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';
}
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?
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 ) ...