-2

I am trying to rewrite the following piece of java code to python:

class pcp {
    public static void main(String[] args) {
        final int b = 13;
        final int m = 1000;
        int a0 = 1;
        int mas[] = new int[10];
        int a = a0, T = 0;
        do {
            System.out.print(a + " ");
            a = (b * a) % m;
            T++;
            mas[a * 10 / m]++;
        } while (a != a0);
        System.out.print("\n" + T + "\n");
        for (int i = 0; i < mas.length; i++)
            System.out.print(mas[i] + " ");
    }
}

It's a simple pseudo random number generator with the following output:

1 13 169 197 561 293 809 517 721 373 849 37 481 253 289 757 841 933 129 677 801 413 369 797 361 693 9 117 521 773 49 637 281 653 489 357 641 333 329 277 601 813 569 397 161 93 209 717 321 173 249 237 81 53 689 957 441 733 529 877 401 213 769 997 961 493 409 317 121 573 449 837 881 453 889 557 241 133 729 477 201 613 969 597 761 893 609 917 921 973 649 437 681 853 89 157 41 533 929 77

100

11 9 11 9 11 9 11 9 11 9 

Here's my Python code:

b = 13
m = 1000
a0 = 1
mas = [0] * 10
a = a0
T = 0

while not (a != a0):
    print(a)
    a = (b * a) % m
    T = T + 1
    mas[int(a * 10 / m)] = mas[int(a * 10 / m)] + 1

print(T)

i = 0
while (i < len(mas)):
    print(mas[i])
    i = i + 1

It simply won't enter the loop an I'm kinda at loss about how rewrite it in the correct way for python.

Thank you for reading and your help.

Caiphas Kain
  • 119
  • 2
  • 10
  • 4
    I don't know python that well but `while not (a != a0)` doesn't seem to be the same as `while (a != a0)`. – Thomas Jul 12 '17 at 12:12
  • 2
    Besides: horrible naming. Probably nobody has told you yet, but **readability** is a core property of source code. Single character variables do not help with that. Use names that **say** what the thing behind the name is about. You see, if that class would be named PseudoNumberGenerator instead of pcp ... you would not have felt the need to tell us what pcp is doing. – GhostCat Jul 12 '17 at 12:18
  • Your code *does* enter the loop. It prints 1. – Denziloe Jul 12 '17 at 12:20

2 Answers2

3

You may want to try changing

while not (a != a0):

to

while (a != a0):

EDIT: this isn't good code. I don't know why you explicitly set 'a' equal to 'a0' but it means that the condition of the while loop is basically useless in the way it's written. Plus, you should read up on naming conventions.

jamlab
  • 78
  • 6
1

You should change while not to just while, both are opposite making your program not to enter the loop at all for that particular condition.

Dragon
  • 1,194
  • 17
  • 24