When does `n` match last digits of `n^2`?

浪尽此生 提交于 2020-07-16 04:45:02

问题


I'm solving one programming problem.

Find all numbers x from 1 to n that have a property that digits of x match digits at the end of x^2.

For example:

5 matches last digit of 5^2 (25)
6 matches last digit of 6^2 (36)
25 matches last digits of 25^2 (625)
76 matches last digits of 76^2 (5776)
376 matches last digits of 376^2 (141376)
625 matches last digits of 625^2 (390625)
etc.

Does anyone know what the numerical criteria is for a number to match itself in last digits of it squared?

I'm programming this in Python and I can only use numeric operations such as /, %, //, *, +, etc.

I can't use str or len or string functions to slice numbers.


回答1:


Say x has k digits. Then x^2 ends with the digits of x if and only if x^2 - x ends with k zeroes, i.e., if x(x-1) ends with k zeroes.

Count the number of times 2 goes into x and into x-1 (the sum), then do the same for 5. If the min of those is at least as big as the number of digits, you have a winner. If not, you don't.

E.g, Consider 5. 5 goes into 5 once and into 4 zero times, 2 goes into 5 zero times and into 4 twice. The min of 1 and 2 is one. 5 has one digit, so you have a winner.


The easy way to find all such numbers between 1 and n is to just check multiples of powers of 5. Since for each k digit number, you need 5^k to be a factor, check 5^k and all multiples that don't exceed k digits. This could be x or x-1, so you'll also need to check the number above.

So the numbers to check are:

k=1: 5, 6
k=2: 25, 26, 50, 51, 75, 76
k=3: 125, 126, 250, 251, 375, 376, 500, 501, 625, 626, 750, 751, ...
k=4: 625, 626, 1250, 1251, ...

etc...

And all you need to check for any of these is if the min set bit gets you at least k 2s.

Note that 625 shows up for both k=3 and k=4, but that's fine since 0625 is valid. In practice you only need to check once, so you can limit yourself to the multiples which are themselves at least k digits.




回答2:


Not a complete criteria, but it can be used as a start point:

First: recall that the last digit in a multiplication is the last digit of the multiplication of the last digit of the first operand times the last digit of the second operand.

As the multiplication is of one number by itsef, the possible combinations are: 0x0, 1x1, 2x2, 3x3, 4x4, 5x5, 6x6, 7x7, 8x8, and 9x9.

The multiplications which have the same last digit as its operands are 0x0, 1x1, 5x5 and 6x6.

So you can start by testing only numbers that end in 0, 1, 5 or 6.

For each of these digits, you can build a two digit number, by prepending a digit 1, 2, 3, ..., 9 to them. So, for digit 0, you have 10,20,30,...,90 . You have to find now which of these numbers, multiplied by itsef, produces a result whose next to last digit is the same as it. Let this two digit number be 10a+b where a is the tens and b is the ones. The value of b is already fixed and is one of 0, 1, 5 or 6. Set a value for b and let (10a+b)(10a+b) which is 100a^2 + 20ab + b^2 be the result of multiplying 10a+b by itsef.

We are interested in the tens of this number, so we divide it into 10, resulting 10a^2 + 2ab + b^2/10 and do a 10 modulus to isolate the tens digit. Substitute the value of b in the expression, performing an integer division for the last term. For example, for b=5, the expression would be (10a^2 + 20*5*a + 2) mod 10. Make this expression to be equal to a and you've got an equation that gives you what values of a match the equality.




回答3:


Hi I wrote this answer:

n=int(input('n='))

for m in range(1, n+1):
    d=m**2
    x = m

    all = 0
    all2 = 0

    while x != 0:
        if x%10 == d%10:
            all += 1
        x = x//10
        d = d//10
        all2 += 1

    if all == all2:
        print m

Please let me know what you think -- it works!




回答4:


Okay This is the edited version:

LastNum=int(input("Enter end limit: "))
def y(num):
    count=0
    while num!=0:
        num=num//10
        count=count+1
    return count
def LastDigitMatch(x):
    if x%(10**y(x))==(x**2)%(10**y(x)):
        return True
    else:
        return False
print('The required list of numbers: ')
for num in range(1,LastNum+1):
    if LastDigitMatch(num):
        print(num,end=', ')

I found Giorgi Nakeuri's version (see comment on Dave Galvin's post) the fastest and the simplest among all. I edited the code:

import math
LastNum=int(input("Enter the end limit: "))
print('The required list of numbers:')    
for x in range(1,LastNum+1):
    if x%(10**math.floor(math.log10(x)+1))==(x**2)%(10**math.floor(math.log10(x)+1)):
        print(x,end=', ')



回答5:


A function which returns a list of these numbers.

def mathcing_squared(n):
    return [x for x in range(1, n + 1) if str(x ** 2).endswith(str(x))]

So if n is 650, it will return

[1, 5, 6, 25, 76, 376, 625]


来源:https://stackoverflow.com/questions/33057999/when-does-n-match-last-digits-of-n2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!