Alice's favorite numbers

Alice loves integers which meet ALL of these criteria simultaneously:

- The integer is greater than 0
- The integer is less than 300
- The integer is odd
- The integer is not prime

For Alice to love an integer, it must meet all of the criteria.

How many integers does Alice love?

You can refer to this list of primes .


The answer is 89.

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.

45 solutions

there are 150 odd numbers and 61 prime numbers between 1-300. so Alice loves 150-61=89 numbers

61 primes that are odd..... u must mention that 61 are odd primes and 2 is an even prime which has been already eliminated .....

Vighnesh Raut - 7 years, 1 month ago

Yes!!!!!

Jeremy Shuler - 7 years, 3 months ago
Taylor Sarrafian
Jul 27, 2013

in Racket:

#lang racket
(require math)

(for/list ([i (in-range 1 300)]
       #:when (and (odd? i) (not (prime? i))))
i)
Jeffrey Robles
Jul 26, 2013

It is not difficult to see that there are 150 150 odd counting numbers less than 300 300 . MatLab has a built-in function that is able to determine the primes less than or equal to a number and return it as a row matrix. Then, the answer should be:

150 l e n g t h ( p r i m e s ( 300 ) ) + 1 = 89 150-length(primes(300))+1= **89** \\

1 1 accounts for the prime number 2 2 which is even.

Yeah! Matlab!

Jeremy Shuler - 7 years, 3 months ago
Otávio Sales
Jul 26, 2013

São 150 números ímpares, de 1 a 299 61 são primos

150-61

Bakshinder Singh
Jul 25, 2013

=150[total odd]-61[odd primes]

Odd number from 1-300 = 150 Odd numbers.Prime numbers from 1-300 = 61 prime numbers.150-61=89

Kashyap Mahler
Jul 25, 2013

There are a 150 odd numbers between 0 and 300. From the list of primes, we calculate the number of primes between 0 and 300 to be 61. Subtracting 61 from 150, we get the answer to be 89

Debjit Mandal
Jul 24, 2013

There are 150 odd numbers in the interval (0,300). There are 62 prime numbers less than 300, of which 61 are odd. From here we subtract the number of odd primes from the number of odd integers to get 89 Alice-compatible integers.

Santanu Banerjee
Jul 24, 2013

As number must be > 0 and less < 300

Thus number lies from 1 to 299

Number of odd numbers from 1 to 298 are 298/2

Thus number of odd numbers from 1 to 299 are 149+1=150

Number of prime numbers less than 300 are 62 BUT THAT INCLUDES 2

As 2 is even thus there are 61 odd primes < 300

Total number of Alice Loving Numbers are 150 - 61 = 89

Saad Haider
Jul 24, 2013

in the language of java, it is this:

import java.io.*;

public class hi { public static void main(String[] args) throws IOException { int count = 0; int counter = 0; for(double i=1; i<300; i++) { if(i%2==1) { for(double j=1; j<=i; j++) { double k = i/j;

                if(k==Math.floor(k)) {
                    double z = k;
                    counter++;
                }
            }
            if(counter != 2) {
                count++;
            }
        }
        counter=0;
    }       
    System.out.println(count);

}

}

Vlad Dogaru
Jul 24, 2013

prime 1 = False

prime n = null $ filter (divisor n) [2 .. (n-1)] where divisor n k = mod n k == 0

loves n = (mod n 2 == 1) && not (prime n)

solution = length $ filter loves [1 .. 300]

Jan J.
Jul 24, 2013

We make simple sieve and loop:

def prime(n):
m = 2
while m < n:
    if n % m == 0:
        return 0
    m = m + 1
return 1

m = 3
s = 1

while m < 300:
    if prime(m) == 0:
        s = s + 1
        print(m,s)
    m = m + 2
print(s)
Razvan Barbu
Jul 24, 2013

The first filter we apply is this: since the numbers are strictly greater than 0 and strictly less than 300 , the interval we will consider is [1 -> 299] .

A recommended next step right now, would be to count all the odd numbers in this interval. Every other number here, starting with 1 is an odd number: 1 2 3 4 5 6 7 8 9 10 etc. As we can see, in a 1 to 10 interval, half of the numbers are odds, which through mathematical induction leads to the conlusion that there are 150 odd numbers in our interval.

Now, the last thing we need to do is apply the last filter, that is to eliminate all the prime numbers among these 150 numbers: 1 3 5 7 9 ... 295 297 299.

If we refer to the list of primes provided by the site, we will see that there are 62 prime numbers between 1 and 300. BUT , there is a but we have to take care of, one these 62 numbers is 2, which we will not consider since it's an even number ( the only even prime number by the way ).

At this is points it's all a matter of fundamental math operation: 150 - 61 = 89.

Kevin Larsen
Jul 23, 2013

150 odd numbers between 0 and 300. 62 prime numbers between 0 and 300. 2 is even, so 61 prime numbers that are odd. 150-61=89

Tyle Stelzig
Jul 23, 2013

There are 150 odd numbers between zero and 300. 61 of these are primes (by looking at list of primes). 150 - 61 == 89.

Takeda Shigenori
Jul 23, 2013

From the list of primes, we know that there are 61 odd numbers fewer than 300 which are prime, and there are 299 1 2 + 1 = 150 \frac{299-1}{2}+1=150 odd numbers fewer than 300. Therefore, there are 150 61 = 89 150-61=89 odd numbers fewer than 300 which are not prime.

R Kumar
Jul 23, 2013

total odds upto <300 are 150 now no. of prime less then 300 is 62 but 2 is only even prime so 61 odd primes so total no thar satisfy condition are 150-61=89

Vivek Sahu
Jul 23, 2013

class abhi

{

public static void main (String args[])

{

int i=0,c=0,d=0,num=0,j=0;

for(i=1;i<300;i++)

{

num=i;

if(num%2!=0)//for odd check

{

for(j=1;j<=num;j++)// for number to be less than 300

{

 if(num%j==0)// for non prime check

 c++;

}

if((c>2)||(c<2))// c<2 to include "1" as non prime

{

 d++;//counting counter increases as soon as all the conditions are met

}

}

c=0;

}

System.out.println("Alice loves "+d+" integers.");

}

}

Muhammad Umer
Jul 23, 2013

make a program and try it!

Evan Chien
Jul 22, 2013

Since all prime integers (except 2) are odd, and there are 61 odd integers less than 300 and greater than 0, you take all the odd numbers less than 300, 300/2 (since half the numbers under 300 are odd), and subtract the primes, 150-61, which equals 89.

Willian Guerreiro
Jul 22, 2013

include<stdio.h>

include<conio.h>

int primo(int i){ int j,m=0; for(j=1;j<=i;j++){ if(i%j==0){ m++; } } if( m==2){ return 1; } else{ return 0; } } main(){ int i=0,a=0; while(i<=300){ if((i%2!=0)&&(primo(i)!=1)){ a++; } i++; } printf("%d",a); getch(); }

Sean Elliott
Jul 22, 2013

My code basically bashed out all the primes, then went through a list of positive integers less than 300 and weeded out the evens and the primes to get the final array.

Randolf Rebrick
Jul 22, 2013
x = []
for n in range(299):
    if (n+1) % 2 and not is_prime(n+1):
        x.append(n+1)
print(len(x))
Ali Rahemtulla
Jul 22, 2013

Seriously, are these jokes? They're just primary school math questions with a bit of a spin. They're rabbits in a sheep's clothing. Scratch that, lamb's clothing

David Harris
Jul 22, 2013

First, find odd numbers between 0-300 exclusive; odds = 150.

Next, find number of primes between 0-300; primes = 62.

Remove prime number 2, the only even prime; primes = 61.

Total loves = odds - primes = 89.

Khaled Adel
Jul 22, 2013

C#: using System; using System.Collections.Generic; class Solution { static void Main (String[] args) { int x; int n; int k=1; bool right=false; Console.WriteLine("1"); for(x=9;x<300;x=x+2){ for(n=3;n<x;n=n+2){ if(x%n==0){ right=true; break; } } if(right){Console.WriteLine(x.ToString ());k++;} right=false; } Console.WriteLine (k.ToString ()); } }

Take the number of the odd numbers bigger than 0 and less than 300 (150), and subtract from it the number of odd primes smaller than 300 (61). The difference is 89.

With a file that has the list of primes given in the exercise, named "primes.txt", here's a solution in python 2.X:

def getprimes(filename):
    f = open(filename)
    primes=[]

    for line in f:
        #Getting rid of the spaces and line ends.
        analyse = line.strip("\n").split(" ") 
        analyse=filter(lambda a: a != "", analyse)
        #Adding those numbers to the list
        primes.extend(analyse)

    return primes

def fav_num(primes):
    selection=[]
    for i in range(0,301):
        if (i%2==1) and (str(i) not in primes):
            selection.append(i)
    return selection

list_primes=getprimes("primes.txt")
fav_numbers=fav_num(list_primes)
print len(fav_numbers)
Ahaan Rungta
Jul 22, 2013
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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
# a list of primes to look over

l = 0 # will count the loved integers
for i in range (1, 151): # scans through the odd integers less than 300
  if not (2*i-1 in p): # checks if not prime
    l += 1 # adds in loved
print (l) # prints answer

The program returns 89 \boxed{89} .

Manoj Pandey
Jul 22, 2013

First, there are 149 even and 151 odd numbers between 1 and 300 (inclusive of 1).

Then a short program in c++ helps:

//Program to check for primality of a number

#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
    int n, count=0,ctr=0;
    for(n=2;n<300;n++)
      {  for(int i=1;i<=n;i++)
         {
                if(n%i==0)
                {
                    count++;
                }
         }
         if(count==2)
        { cout<<n<<"\t"; 
        ctr++;
        }
        count=0;
      }
    cout<<"Total primes between 1-300 are: "<<ctr;
}

The program gives all prime numbers between 1 and 300 and also the total numbers i.e.62.

Aliter: You can easily calculate total primes from the link given in the question.

Thus, 151-62=89.

Alice loves 89 89 integers.

Update: There is a typo there.

There are 150 even and 150 odd numbers between 1 and 300 (inclusive of 1). Removing the '2' we get from the program's output, we have 61 primes.

So, total non-prime numbers= 150-61= 89 89

Manoj Pandey - 7 years, 10 months ago
Yun Kai Lim
Jul 22, 2013

Loop for all odd integer from 1 to 299, test each number whether it is a prime, if it is not a prime +1 to the counter.

We can use the most simple prime testing function, which try divide the number with each number lesser then it(except 1), if it is divisible by one of them then it is not prime.

Leonardo Cidrão
Jul 22, 2013

1) In the list of primes, there are 62 minor than 300.

2) There are 150 odd numbers between 0 and 300.

3) Excluding 2 from the 62 primes, we have: 150-61=89.

Alex A.
Jul 22, 2013

Haskell:

module Alice where

isPrime :: (Integral a) => a -> Bool
isPrime 1 = False
isPrime x = null [ k | k<-[2 .. x-1], x`mod`k == 0]

numAliceLoves :: Int
numAliceLoves = length [x | x <- [1 .. 299], x`mod`2 == 1, isPrime x == False]

counter=0 for i in range(0,300):

counter_prime=0
for j in range(1,i):
    if(i%j == 0):
        counter_prime=counter_prime+1

if(counter_prime>1):

    if(i%2!=0):
        counter=counter+1

print counter

The above code excludes 1. But 1 satisfies the conditions and hence the solution is 88+1=89

Dicky Djayadi
Jul 22, 2013

In a loop in range 1 to 300, check whether the current number isn't a prime and is an odd number. It results 89. I did a pre-computation for the prime checking.

Nelvson Shine
Jul 22, 2013

between 0 - 300, there is 150 odd numbers. it means the correct answer is less than 150. and between 0 - 300, except for 2 2 , there is 61 prime numbers ( you can prove it here ) .

Thus, the total integers Alice loves is 150 61 = 89 150 - 61 = 89

Hans Ardisa
Jul 22, 2013

MANUAL ...KULI

or

1-300,there are 150 odd number and at 1-300 range there are 62 prime,2 is out,so there are 61 odd prime number..150-61=89

Yea just like me

Hafizh Ahsan Permana - 7 years, 10 months ago
Robbert Westerink
Jul 22, 2013

there are 150 odd numbers between 1 and 300 and 62 primes under 300. We have to remove the even prime (2) and hence substract 61 from 150 = 89.

Ruslan Abdulgani
Jul 22, 2013

From 1 to 300, there are 150 even numbers and 150 odd numbers. From the odd numbers, there are 61 prime numbers. So the integers that Alice loves are 150 - 61 = 89.

Zi Song Yeoh
Jul 21, 2013

Here is a non-programming solution:

By looking at the list of primes, we can see that there are 62 primes less than 300. (this is easy to see since the primes are grouped in rows of 10. Note that 2 is an even prime. So, there are 61 odd primes less than 300. There are 150 odd numbers between 0 and 300. Thus, the number of odd composites (or lucky numbers) are 150 - 61 = 89.

Akshay Mehra
Jul 21, 2013

we have 150 odd numbers in the list of which 61 are primes simply answer will be 150-61=89

Mayank Kaushik
Jul 21, 2013

import math

M = 300

sqM = int(math.sqrt(M)) + 1

prime=[0 for i in range(0,M)]

prime[0],prime[1]=1,1

''' sieve of Eratosthenes '''

for i in range(2,sqM):

if prime[i] == 0:
    j = 2
    while i*j<M:
        prime[i*j]= 1
        j += 1

n = 0

for i in range(1,300,2):

if prime[i] == 1 :
    n += 1

print(n)

Yihang Ho
Jul 21, 2013

From 1 to 299, there are 150 odd numbers, and out of these 150 odd numbers, there 61 of them are primes. Hence, the number of integers fulfilling the given conditions is 150 61 = 89 150-61=89 .

isn't there 62 primes?

Jian Feng Gao - 7 years, 10 months ago

Log in to reply

Yes, but we are considering only odd primes.

Yihang Ho - 7 years, 10 months ago

how do you know there are 61 odd primes? do you have counted them?

Jose Mateos - 7 years, 10 months ago

Log in to reply

There is a list of primes given isn't it?

Yihang Ho - 7 years, 10 months ago

Log in to reply

Yes, but i think there should be a way that doesn't involve counting every single odd prime that is on the given list.

Jose Mateos - 7 years, 10 months ago

Log in to reply

@Jose Mateos I don't think there is a good way to calculate the numbers of primes. And I guess for problems with small cases like this, counting is much faster than writing a program to solve.

Yihang Ho - 7 years, 10 months ago
Andrias Meisyal
Jul 21, 2013

You can solve this problem using brute force method, just run the code that meet the requirement (skip the prime requirement), like this:

*Finding how many odd numbers between *0 < i < 300 **

hasil = 0

for i in range (1,300):

if i%2 == 1:

    hasil = hasil + 1

print(hasil)

you can get 150 odd numbers. Then, count manually that odd and prime between that is 61 numbers. So, the answer odd and not prime is 150 - 61 = 89 . But, there must another efficient algorithms.

Thank you!! But don't you agree that they should make some sort of class for prime numbers in Obj. Orientated Coding?

David Kroell - 7 years, 10 months ago

Log in to reply

PS: my code is Python version

I agree that using OOP for prime numbers. But, until now, I am still studying about how to get/check prime numbers. I know that there is Sieve of Eratosthenes which the best algorithm finding prime number (I don't implement yet).

Andrias Meisyal - 7 years, 10 months ago

Log in to reply

Not best , but it's rare that one needs so many primes to make Sieve of Atkin significantly faster than Sieve of Eratosthenes (or to make it worth the time spent to implement Sieve of Atkin).

However, for checking primality, there are a bunch of algorithms way faster than Sieve of Eratosthenes. Even trial division is faster than Sieve of Eratosthenes, because we don't need to generate the previous primes.

Ivan Koswara - 7 years, 10 months ago

Log in to reply

@Ivan Koswara I am sorry, thanks for clarification, Ivan K. Nice to discuss.. I just known too, that, there are many algorithms there..

Andrias Meisyal - 7 years, 10 months ago
Kelson Ball
Jul 21, 2013

Code creates an integer array with all prime numbers up to 307, and then runs from 0 (0 is even) to 299 using a for loop . The outer if checks to see if the number is prime. Instead of searching the entire list, it checks for the next prime number using the integer variable primeindex . If the number is prime, it increments primeindex . If not prime (" else ") then it checks to see if the number is odd using the moduland operator (n%2 is equal to 1 fort all odd numbers). If the number is also odd it increments the integer variable sum . The last line of code prints the variable sum.

//In Java
int[] primes = {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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307};\

int sum = 0; int primeindex = 0;

for (int i = 0; i < 300; i++){ if (primes[primeindex] == i){ primeindex++;
} else { if (i%2 == 1){ sum++; println(i + " is loved"); } } } println(sum);

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...