Brief Prime Product Problem

How many integers from 1 1 to 100 100 inclusive can be written as the product of two (not necessarily distinct) primes?


If you can find a computer science or combinatorics approach, post your solution!


The answer is 34.

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.

22 solutions

Mahdi Al-kawaz
Apr 18, 2014

Listing the primes from 2 to 47 then you can find:

  • 2 can multiply by 15 numbers

  • 3 can multiply by 10 numbers

  • 5 can multiply by 6 numbers

  • 7 can multiply by 3 numbers

The answer is 34 \boxed {34}

I used the method of listing the primes, and finding numbers with exactly 4 factors. This totaled to 31 31 . This is when the primes are distinct. If the primes are the same, then the squares of primes must be included. Thus, the squares of 2 , 3 , 5 , 7 2, 3, 5, 7 are included. Thus, the total I reached was 35 \boxed{35} .

Could someone point out the mistake in my logic?

Nanayaranaraknas Vahdam - 7 years, 1 month ago

Log in to reply

Could be computation. Like you'd already counted a perfect prime square.

Finn Hulse - 7 years, 1 month ago

I think you just miscounted, there should be 30 numbers that are products of 2 distinct primes. Your logic should be correct, but you could've just counted the numbers with 3 or 4 factors instead.

Andrew Carratu - 9 months, 1 week ago

Did you include 1?

a t - 2 months, 3 weeks ago

Excellent job! :D

Finn Hulse - 7 years, 1 month ago

  1. http://pastebin.com/yCimkjBz

Ashini Singha - 6 years, 9 months ago

Does that count the repeated operations? (Such as 2x7=7x2)

Andy Arteaga - 2 years, 11 months ago

Log in to reply

no because both would be 14

Andrew Carratu - 3 weeks, 3 days ago
Thaddeus Abiy
Apr 17, 2014

Since coding is allowed,here is the simplest thing that came to my mind

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from itertools import *
def isprime(n):
    for i in range(2,int(n**.5))+1):
        if n%i==0:return False
    return True
Integers = set([])
for a,b in product(range(2,100),repeat=2):
    if a*b <= 100 and isprime(a) and isprime(b):
        Integers.add(a*b)
print len(Integers)

Brilliant & simple

Mahdi Al-kawaz - 7 years, 1 month ago

Brilliant! :D

Finn Hulse - 7 years, 1 month ago

There's an extra ) after n**.5

Aniket Bhattacharyea - 4 years, 9 months ago

I simplified it to between 2-50 but great

john cenator - 4 years, 5 months ago

Mathematica one liner: Length@Select[Range@100,PrimeOmega@#==2&]

Χωρις Ονομας - 3 years, 6 months ago
Anand Raj
Jul 5, 2014

2 Can be Multiplied with all Primes Upto 47.... = 15

3 Can Multiplied with all primes upto 31 except (2).. = 10

5 Can be Upto 19... = 6

7 Can be Upto 13... = 3.....

Adding All we get ::34::

Please Solve This Problem

For your problem..the relative rate of change of population would be (1/33-1/46)(say A). So after n years total population will be ((1+A)^n)*Initial population. So (1+A)^n=2... N=log(2)/log(1+A)... n=81.28 ... Rounding off n=82

Poojan Pujara - 2 years, 2 months ago
Kim Fierens
Apr 24, 2014

Mathematica solution:

Length[DeleteDuplicates[ Select[Flatten[ Table[Prime[i] Prime[j], {i, 1, 4}, {j, 1, 15}]], # < 100 &]]]

This gives 34.

Nice.

Kp Govind - 7 years, 1 month ago

Or with FactorInteger:

Length@Cases[Total[#[[All, 2]]] & /@ FactorInteger /@ Range[100], 2]

Frederik Van der Veken - 3 years, 7 months ago
Bethany Waanders
Nov 15, 2020

combinatorics approach

Mostafa Balboul
Mar 28, 2016

We can split our composite numbers into two varieties: No factors are greater than the square root, or, One factor is greater than the square root.

Finding all of the first variety boils down into finding all possible combinations of two prime numbers lower than the square root of our upper bound. In this case, that number is 10. We have four prime numbers less than ten, competing for two slots, so it ends up evaluating to (n+r-1)!/(n-1)!r! = 5!/3!2!, or 10 ways.

For the second variety, one factor is greater than the square root. As a result, one factor is smaller than the square root. For each of these four factors, we must assess how many numbers they can be multiplied by to get a number below 100. For example, 7 can be multiplied by any number below 15.

7 can be multiplied by 2 numbers above 10. 5 can be multiplied by 4 numbers above 10. 3 can be multiplied by 7 numbers above 10. 2 can be multiplied by 11 numbers above 10.

Overall, this adds up to 10 + 2 + 4 + 7 + 10 = 34.

Jason Williams
Dec 21, 2015

Define the problem as the product of two primes. Every such product has a left and right factor. let the left factor be necessarily smaller than the right. To find the least upper bound of the left factor, take the square root of 100. This is 10, so the left prime can be at most 7. Next split the problem into four cases since the left factor can only be 2, 3, 5, or 7. Each case shall be defined by the left factor. Now to find the least upper bound of the right factor for each case, divide 100 by each left factor in turn and take the integer part. These results are 50, 33, 20, and 14. There are 6 primes between 2 and 14, 2 between 15 and 20, 3 between 21 and 33, and 4 between 34 and 50. If the left factor is 2, the right factor can be in any of these categories. For each successive case, eliminate the last category from the preceding case. the sum is 40. Now eliminate repeated products, of which there are 6. Therefore, there are 34 possibilities

Didn't check the exact math, but the method you used was perfect. Great job!

Finn Hulse - 5 years, 5 months ago

how do you determine number of repeats?

Hasan Abdullayev - 3 years, 7 months ago
Bill Bell
Jan 13, 2015

The Python sympy library will calculate the prime factors of a number and put them into a dictionary for you.

Finn Hulse
Apr 17, 2014

I honestly just listed all of them out. They are: 4 , 6 , 9 , 10 , 14 , 15 , 21 , 22 , 25 , 26 , 33 , 34 , 4 ,6, 9,10, 14, 15, 21, 22, 25, 26, 33, 34, 35 , 38 , 39 , 46 , 49 , 51 , 35, 38, 39, 46, 49, 51, 55 , 57 , 58 , 62 , 65 , 69 , 74 , 77 , 82 , 86 , 87 , 55, 57, 58, 62, 65, 69, 74, 77, 82, 86, 87, 91 , 93 , 94 , 95 91, 93, 94, 95 . I'm really interested in finding a cool approach to this problem other than brute force. Note that any prime numbers are automatically disqualified, because remember, 1 1 isn't prime. Have fun thinking of a better solution, and tell me if I missed any so I can update the answer! :D

@Calvin Lin I'd really appreciate it if you reshared this problem. I think it's really great but I don't want it to fall under the radar like my other problems. Thanks! :D

Finn Hulse - 7 years, 1 month ago

Listing Out Takes Time,But Still Is A Good WAy!..:)..Worked Here as Well!

Harikrishna Menon - 7 years, 1 month ago

I did it the same way

Abdur Rehman Zahid - 6 years, 7 months ago

hi, you missed 85=5*17

Hasan Abdullayev - 3 years, 7 months ago

Log in to reply

You are right, he listed only 33 numbers. 85 missing.

a t - 2 months, 3 weeks ago
Wolfgang Teuber
Aug 21, 2018

Using Ruby , the following script will give you 34 :

1
2
relevant_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
relevant_primes.repeated_permutation(2).map { |a, b| a * b }.select { |n| n <= 100 }.uniq.count

Leor Fishman
Feb 24, 2018

A solution in Ocaml: utop # let primes = [2;3;5;7;11;13;17;19;23;29;31;37;41;43;47]

val primes : int list = [2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47]

utop # let divwithmod i j = if (i mod j != 0) then None else Some (i/j);; val divwithmod : int -> int -> int option = <fun>

utop # open List;; ─( 22:45:31 )─< command 3 utop # let deoption a = match a with |Some b -> b |None -> 999;; val deoption : int option -> int = <fun>

utop # let primecheck i = fold_left ( || ) false (map (fun x -> mem (deoption (divwithmod i x)) primes) primes) val primecheck : int -> bool = <fun>

utop # let rec totprime curr tot = if curr >= 100 then tot else if primecheck curr then totprime (curr+1) (tot+1) else totprime (curr+1) tot;; val totprime : int -> int -> int = <fun>

utop # totprime 2 0;;

Bruce Karsh
Jan 20, 2018

Simple Python brute force solution:

p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
print len([m*n for m in p for n in p if m <= n and m*n < 100])
Pierre Lapôtre
Aug 8, 2017

Alfredo Saracho
Aug 6, 2017

Mathematica code

cont=0;
For[i = 1, Prime[i] <= 100, i++, {For[j = i, Prime[j] <= 100, j++, {
    If[Prime[i]*Prime[j] <= 100, cont++]}]}]

cont
Angel Krastev
Dec 24, 2016

Here is a code in Just BASIC:

  data 15, 2, 3, 5, 7,11,13,17'we only need 15 primes

  data 19,23,29,31,37,41,43,47'from 2 to 47 so that

  'no product of smallest and biggest goes above 100

  read size:dim primes(size)'size = 15

 for i= 1 to size'put 15 primes in primes()

    read prime:primes(i)=prime

 next i

 'making all possible pairs from 15 primes

for first = 1 to size

 for second = first to size

  product = primes(first) * primes(second)

  if product <= 100 then count = count + 1

 next second

next first

print "count = ";count

It gives total of 34.

Nils Gösche
Jul 1, 2016

Some Lisp Code:

(length (let ((primes '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47)))
          (mapcon (lambda (sub)
                    (let ((head (car sub)))
                      (mapcan (lambda (elt)
                                (if (<= (* elt head) 100)
                                    (list (cons head elt))))
                              sub)))
                  primes)))

Evaluates to 34.

Suvaditya Sur
Jun 11, 2016
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
require 'prime'
arr = Prime.take(100)
lst = []
for i in (0...100) do
   for j in (0...100) do
      if(arr[i] * arr[j] <= 100) then
         lst << arr[i] * arr[j]
      end
   end
end
lst.uniq!
puts "#{lst.size}"

I run it in C++, little brute force to find the primes,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<bits/stdc++.h>
using namespace std;

int main(){
int array[15]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; //prime under 50
int shell,count;

for(int i=0;i<15;i++){
    for(int j=i;j<15;j++){
        shell=array[i]*array[j];
        if(shell<=100){
            count++;
        }
    }
}

cout<<count<<endl;

}

Here is the VB.NET code I used. Pardon me for my choice of variable names - I admit their pretty bad. Also, I skipped 1 for obvious reasons.

Module Module1

Sub Main()        
    Dim number_of_numbers_counter As Integer = 0
    Dim counter As Integer = 2
    Dim counter_2 As Integer = 0
    For n = 2 To 100
        Dim prime_factors(0) As Integer
        Dim n_editable As Integer = n
        While Not n_editable = 1
            If n_editable Mod counter = 0 Then
                n_editable /= counter
                prime_factors(counter_2) = counter
                ReDim Preserve prime_factors(prime_factors.Length)
                counter_2 += 1
            Else
                counter += 1

            End If

        End While
        ReDim Preserve prime_factors(prime_factors.Length - 2)
        If prime_factors.Length = 2 Then
            Dim number1 As Integer = prime_factors(0)
            Dim number2 As Integer = prime_factors(1)
            number_of_numbers_counter += 1
            If isPrime(number1) And isPrime(number2) Then
                Console.WriteLine(n.ToString + "   " + number1.ToString + "   " + number2.ToString)

            End If
        End If
        counter = 2
        counter_2 = 0
    Next
    Console.WriteLine()
    Console.WriteLine(number_of_numbers_counter)
    Console.ReadLine()
End Sub
Public Function isPrime(ByVal Number As Integer) As Boolean
    Dim factors As Integer = 0

    For n = 1 To Number
        If Number Mod n = 0 Then
            factors += 1

        End If
    Next
    If factors > 2 Then
        Return False
    Else
        Return True
    End If
End Function
End Module
Sajib Khan
Sep 20, 2014

We can easily generate primes from 1 to 100 by sieve method. Here, I just assign the primes into prime[] array. It's C++ code.

prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71, 73,79,83,89,97}
l=25; \ \ \ \ \ \ \ \ \ \ // size of prime[]
for(i=0; i<l; i++)
{
for( j=i; j<l; j++ )
{
n = prime[ i ] * prime[ j ];
if( n < 100)
answer.push_back(n); \ \ \ \ \ \ \ \ \ // value are putting in answer vector.
}
}
printf("The total number is = %d",answer.size());










Arpit Kothari
Jul 6, 2014

just list down all the primes upto 50 2,3,5,7,11,13,17,19,23,29,31,37,41,43 and 47 Total = 15

Now 2 can be multiplied by other 14 to give me result<100 Similarly,
3 will give me 8 solutions

5 will give 5 solutions

7 will give me 2 solutions and

11 will give me only 1 solution. Adding all I will get 30 solutions.

In addition to it, I can also have squares of 2,3,5 and 7 as my solution. Therefore, adding all I will get total 34 solutions

3 will give you 9 solutions; 11 will give you 0 solutions (11*13=143).

David Hammer - 5 years, 5 months ago
Jeet Mohapatra
Jul 3, 2014

For an integer to be <100, 1 factor needs to be <10 and other >10. There are 15 prime numbers <50. There are 4 prime no.s <10 (2,3,5,7). The other no. Has to be >10 and - For 2, <50 . So 15-4= 11 no.s..... For 3, <33 . So prime no.include 11,13,17,19,23,29,31. So 7 no.s..... For 5, <20 . So 4 no.s.... For 7, <14 . So 2 no.s.... So total there are -11+7+4+2 numbers = 24 no.s Again 2,3,5,7 can be selected in 4C2 ways = 6. And finally, when digits are repeated , there are 4 no.s (2,3,5,7). Altogether number of ways= 24+6+4=34 ways.

// implementation using c++

include <iostream>

using namespace std; int main()

{

int A[16] = {0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};

int c = 0;
for (int i = 1 ; i <= 100; i++)
{
  bool flag = true;
 for (int j = 1 ; j <= 15 && flag ; j++)
     for (int k = 1 ; k <= 15 && flag ; k++)
         if (i == A[j] * A[k]) { c++; flag = false; }
}

cout << c;

}

Sami Mohamed Mansour - 4 years, 2 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...