Array equal

I have a function where I evaluate two arrays. For some reason, I got wrong not equal. Even when I evaluate element by element.
What is wrong?
Here is my code:

    Number = 3
    n = 15
    U = [1,3,5,7]
  
    ab = Vector{Int64}()
    
    for i=0:20
        temp = mod(Number^i,n)
        
        positionArray = findfirst(isequal(temp),ab)
        
        if positionArray .== nothing 
            push!(ab,temp)
        end
        x = sort(ab)
        
        if x == U 
            println(true)
            break
        end

        for j=1:size(x,1)
            println(U[j])
            if x[j]==U[j]
                println(true)
            else
                println(false)
            end
        end
    end

How can you tell if you are getting a wrong equal? Your function doesn’t return any information about whether x and U are equal. It just returns sort(aa), and aa isn’t defined anywhere. Is that a typo?

ok, remove function. I just tried to make that function work.

U has the numbers 1, 3, 5, 7, but mod(3^n, 15) contains the numbers 1, 3, 6, 9, 12. So they are not equal:

jl> sort(mod.(3 .^(0:20), 15))
21-element Vector{Int64}:
  1
  3
  3
  3
  3
  3
  6
  6
  6
  6
  6
  9
  9
  9
  9
  9
 12
 12
 12
 12
 12

And the loop how is working ? Evaluating as it should?

There is a couple of things that are strange here.

Don’t name a variable Number. That’s a basic type. It shouldn’t cause any problems, but it will be confusing to read.

positionArray will be a scalar, so don’t use .== to compare it (Again, this is just for clarity for anyone reading your code). isnothing(positionArray) is a good way to check for nothingness.

There will be an overflow error every time you input a vector that isn’t equal. But you know how long the vector should be.

Instead of adding the j loop (which I assume is an afterthought when you didn’t get the result you expected), you could have added a couple of println(x);println(U)s.

If this snippet does what I think it does, you could replace it by

number = 3
n = 15
U = [1,3,5,7]
U == sort(mod.(number.^(0:(length(U)-1)), n))
2 Likes

Nice syntax!

My mistake was that in the original function I didn’t use return, just true and false.
Thank you for your help!