Solving a problem using a program
Many programming languages support working with large numbers. Using such languages, one can solve similar problems by writing a program.
When using
PascalABC.net you can use the
System.Numerics.BigInteger
type (designed for arbitrarily large signed integers) and its associated functions. Due to the peculiarities of working with numbers, you must use
BigInteger.Pow(9,34)
to calculate the degree.
The Java programming language can also handle large numbers using the BigInteger
type.
The programming language Python allows you to work with large numbers, as with "ordinary" numbers. Therefore, you don't even have to think about the fact that this requires more memory.
Algorithm
1. Store the result of the given arithmetic expression in a variable.
2. Using the while
loop to simulate the long division method, we calculate all the digits of the number and count the necessary ones.
Some Python syntax needed to write this kind of program:
Arithmetic operations
addition -
+
subtraction -
-
exponentiation -
**
multiplication-
*
normal division -
/
integer division -
//
calculating the remainder of a division -
%
Python doesn't have brackets. It is necessary to indent to highlight the loop body, forks, subprograms. It is recommended to use 4 spaces for each indentation level.
Conditions
if condition:
action
else:
action
Logical operations
negation - not
logical multiplication - and
logical addition - or
equality - ==
not equal - !=
while loop
while condition:
loop_body
for loop
for i in range(start, finish, step):
loop_body
For example, loop through all elements of array A:
for i in range(len(A)):
A[i] = ...
start - if not specified, it is equal to 0
step - if not specified, it is equal to 1.
Declaring an array of dimension n, with zero initial values
a = [0]*n
Example
Arithmetic expression value: \(49^7 + 7^{21} – 7 \)– written in the number system with base 7. How many digits 6 are contained in this entry?
When calculating numbers in a number system with base 7, it is necessary to find the remainder when dividing the number by 7. The number itself is constantly divided by 7 until we get 0.
n = 49**7 + 7**21 - 7
k6 = 0
while n!=0: # can be written shorter: while n:
if n % 7 == 6:
k6 += 1 # short for k6 = k6 + 1
n //= 7 # short for n = n // 7
print(k6)
If you are not familiar with the Python syntax, but want to use it to solve USE problems, then we have Courses and Tutorial to get you up to speed on the syntax.