0

Even and square numbers

The data.txt file is located in the following lines of 1000 numbers from the range [0.999999].

(a) to the a.txt file, enter the number of even numbers in the data.txt file in the following form: "Even numbers are [number of numbers]" //ivedoneit

(b) to the b.txt file, copy all numbers from the file data.txt, in which the digit of unity is equal to 7 or 0//ivedoneit

(c) to the c.txt file, copy all numbers that are squares of integers, eg this number is the number 225, because

225 = 15**2

I've done already

def main():

    infile = open('dane.txt','r')


    evenTotal = 0
    oddTotal = 0

    line = infile.readline()

    while line != '':
        if int(line) % 10 == 0 and int(line) % 7 == 0:
            evenTotal += int(line)
            even = open('a.txt', 'w')

            even.write("wszystkie liczby jednosci ")
            even.write(str(evenTotal))

        else:
            oddTotal += int(line)
        line = infile.readline()
    print('The total for the even numbers is',evenTotal)
    print('The total for the odd numbers is',oddTotal)

    infile.close()


    print('All done!')

main()

its good for (b) for (a) I have only to change for

if int(line) % 2 == 0:

and it works but I completely don't know how to do (c).

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50

2 Answers2

0

There are a finite number of squares of integers in between 999999. You can put together a set of all integers in between 0 and 999999, then for each line check if the line's number is in this set.

C_Z_
  • 7,427
  • 5
  • 44
  • 81
0

Assisted from Python: Check if a number is a perfect square

You should run:

if int(math.sqrt(int(line))+.5) ** 2 == int(line):
jadki
  • 482
  • 1
  • 8
  • 15