Matrix of Primes

[ 3 5 2 7 ] \large {\begin{bmatrix} 3 & 5 \\ 2 & 7 \end{bmatrix}}

Above is a 2 × 2 2 \times 2 matrix having elements from the set of first 4 4 prime numbers i-e S = { 2 , 3 , 5 , 7 } S= \left\{ 2,3,5,7 \right\} such that they may or may not repeat.

For how many ordered tuples T ( w , x , y , z ) T(w,x,y,z) , where w , x , y , z w,x,y,z belongs to the set S S , determinant of the matrix M = [ w y x z ] M=\begin{bmatrix} w & y \\ x & z \end{bmatrix} is also prime ?


This problem is a part of set Prime Crimes via Computer Science


The answer is 35.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
s=[2,3,5,7]
def dt(ls):
    w,x,y,z=ls[0],ls[1],ls[2],ls[3]
    return w*z-x*y
from gmpy import is_prime
co=0
for a in s:
    for b in s:
        for c in s:
            for d in s:
                if is_prime(dt([a,b,c,d])) and dt([a,b,c,d])>0:              #negatives are not counted
                    co+=1
print(co)

Instead of bring lazy and using gmpy , I'd have just listed primes up to 43 43 as largest determinant is 45 45 .

展豪 張
Mar 23, 2016

No solution...just brute force it :)

What if you followed the solution. How would you do it? :P

Zeeshan Ali - 5 years, 2 months ago

Log in to reply

What do you mean by followed the solution?

展豪 張 - 5 years, 2 months ago

Log in to reply

What if you wanted to follow an algorithm? How would you do it? :)

Zeeshan Ali - 5 years, 2 months ago

Log in to reply

@Zeeshan Ali I'm using python here (pseudo-code) (maybe a stupid approach):
Prime_list=[2,3,5,7,...,43] (max = 7 2 2 2 = 45 =7^2-2^2=45 , max prime under 45 45 is 43 43 )
Count=0
Run w , x , y , z w,x,y,z through { 2 , 3 , 5 , 7 } \{2,3,5,7\} (four-layer for-loop)
if determinant in Prime_list, Count+=1
print(Count)
=D



展豪 張 - 5 years, 2 months ago

Log in to reply

@展豪 張 That's it.. you are good :)

Zeeshan Ali - 5 years, 2 months ago

Log in to reply

@Zeeshan Ali Thank you. This is a good question. Actually the whole set is challenging!

展豪 張 - 5 years, 2 months ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
using namespace std;

int comp(int &n)                //function for checking whether number is prime or composite
{
    int s=0;
    for(int i=1;i<=n;i++)
        {
            if(n%i==0)
                    s++;        //calculates no. of divisors
        }

    if(s==2)                    //divisible by 1 and the number itself
            return 1;
    else
        if(s>2)                  //divisible by more numbers other than 1 and the number itself
                return 0;
        else                    //elimination of 1 as it is not a prime number
                return 0;
}
main()
{
    int a[4],i=0,j=0,k=0,m=0,t=0,d;
    a[0]=2;                         //array
    a[1]=3;                         //of
    a[2]=5;                         //the
    a[3]=7;                         //4 primes

    for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
                {
                    for(k=0;k<4;k++)
                        {
                            for(m=0;m<4;m++)
                                {
                                    d=a[i]*a[m] - a[j]*a[k]; //determinant
                                    if(comp(a[i]) && comp(a[j]) && comp(a[k]) && comp(a[m]) && comp(d))
                                        {
                                            //cout<<a[i]<<" "<<a[j]<<" "<<a[k]<<" "<<a[m]<<endl; //optional statement to print the tuplet
                                            t++;
                                        }
                                }
                        }
                }
        }

    cout<<"\nTotal = "<<t;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...