Fake Coin Problem (2024)

Fake Coin Problem (1)

Get this book -> Problems on Array: For Interviews and Competitive Programming

We will see what fake coin problem is and will also see an efficient method to solve the problem.

Table of contents:

  1. What is Fake Coin Problem?
  2. Brute force method
  3. Decrease and Conquer Technique
  4. Time and Space Complexity

Fake coin problem is an interesting problem in which we have to find a fake coin out of a number of coins, which is assumed to be lighter than the real coins using just a balance scale, which can be used to compare the weights of two piles of coins.

There can be two ways of solving this problem , one can be the brute force one and the other one will be the more efficient one.
We will analyse both one by one.

In this method, we will pick up a coin at random and use it as a test coin for other remaining coins.
We will test it with other coins one by one and if in any case the balance is seen to be tilting on one side then, the fake coin will be present in that case and the one which is lighter will be the fake coin as per our rule.
If the test coin itself is lighter then, we will be able to identify on the first go, as the balance scale will get tilted towards the heavier coin.

Algorithm:-

1.First coin from the top(any coin can be test coin here we are taking the first coin) will be chosen as the test coin against which every other coin will be tested.
2.Firstly, we will compare first and second coin, if they will be balanced, then we will proceed to compare first and third coin, and then first and fourth coin and then we carry on subsequently until we get an unbalance in the balance scale.
3.Then the coin with which we have compared the first coin will be our fake coin.

Example:-

Suppose the weights of the coin are 2 units for the real one and 1 units for the fake one and there are 10 coins stored in form of a pile, of which 6th coin is the fake one(which we don't know beforehand, this information is given here just for the sake of understanding what is happening).
According to the above algorithm, we will first compare the first coin with second coin in the pile, and then carry on subsequently until we get any unbalance and in this case that will be the 6th coin, thus, 6th coin will be the fake one.
Fake Coin Problem (2)

import randomreal_coin = 1fake_coin = 0#Shuffles the coin so that we stimulate the environment that we don't know #the fake coindef randomize_coins(n): """A shuffled list of (n-1) real coins and 1 fake coin.""" global coins coins= [real_coin] * (n - 1) + [fake_coin] * (1)# Generate an array of coins random.shuffle(coins) print(coins) return coinsdef testing_fake_one(n): for i in range(1,n): if coins[0]==coins[i]: pass elif coins[0]<coins[i]: return print(1,"th coin is the fake one") else: return print(i+1,"th coin is the fake one")n=int(input("Enter the number of coins:"))randomize_coins(n)testing_fake_one(n)

Time complexity for the implementation of the above algorithm is O(n), where n is the number of coins, out of which fake coin is to be determined.
Space complexity is also O(n).

The type of Decrease and Conquer technique we are going to use is the one in which we divide the problem size by a constant factor to get certain subdivisions and ignore the rest while solving for the one subdivision which is suitable for our algorithm like binary search, while in the case of divide and conquer we try to solve for all the subdivisions of the problem like in Merge Sort.

Here is an algorithm:

  1. Take number of coins, n, as an input, along with their weight with fake one having different weight than the real ones.
  2. If only one coin is there then that will be the fake coin.
  3. If more than one coins are there then divide the coins into piles of A = ceiling(n/3), B=ceiling(n/3), and C= n-2* ceiling(n/3).
  4. Weigh A and B, if scale balances then repeat from the first step with total number of coins equalling number of coins in C.
  5. If the scale unbalances then repeat from the step 1, with the lighter of A and B.

If n mod 3=0, then three piles will be of size k,k,k.
If n mod 3=1, then three piles will be of size k,k,k+1.
If n mod 3=2, then three piles will be of size k+1,k+1,k.

E.g.:- Suppose there are 12 coins of which 11 are real and one of them is lighter, then, find out the fake one in least weighings possible.
Since, there are 12 coins which is divisible by 3, therefore, we divide by 3, and make piles of size 4, namely A,B and C.
Now, only 3 situations are possibl

  1. Compare A and B, weight of A>B.
  2. Weight of A<B.
  3. Weight of A=B.
    If A>B, then, clearly the counterfeit coin is in pile B, again, since pile B has 4 coins and we know that 4 mod 3=1, thus, we create piles of size 1,1, and 2.
    Similarly, if A<B, then, clearly the counterfeit coin is in pile A and we will do the same step as mentioned in A>B.
    And, if A=B, then, clearly the counterfeit coin is in pile C and again we will again divide the pile C in groups as mentioned in the case where A>B.
    We compare piles of size 1 and 1. Again, only 3 situations are possible, i.e. if one of them is lighter then that one is the fake coin or if both are equal in weight then, fake coin must be in the pile having size 2 and, thus, we again, divide 2 coins into 3 groups namely of size 1,1 and 0, and we compare the piles of size 1, the one which is lighter is the fake one.
import randomreal_coin = 1fake_coin = 0def randomize_coins(n): global coins coins= [real_coin] * (n - 1) + [fake_coin] * (1) random.shuffle(coins) print(coins) return coinsdef testing_fake_one(x,y,n): if n==1: print(1,"st coin is the fake one") else: if n % 3==0 or n%3==1: A=[coins[i] for i in range(x,x+(y-x)//3 )] B=[coins[i] for i in range(x+(y-x)//3,x+2*(y-x)//3)] C=[coins[i] for i in range(x+2*(y-x)//3,y)] coins_index_A=[i+1 for i in range(x,x+(y-x)//3)] coins_index_B=[i+1 for i in range(x+(y-x)//3,x+2*(y-x)//3)] coins_index_C=[i+1 for i in range(x+2*(y-x)//3,y)] if sum(A)<sum(B): if len(A)>1: testing_fake_one(x,x+(y-x)//3,(y-x)//3) if len(A)==1: return print(coins_index_A[0],"th coin is the fake coin") elif sum(A)>sum(B): if len(B)>1: testing_fake_one(x+(y-x)//3,x+2*(y-x)//3,(y-x)//3) if len(B)==1: return print(coins_index_B[0],"th coin is the fake coin") else: if len(C)>1: testing_fake_one(x+2*(y-x)//3,y,y-2*(y-x)//3-x) if len(C)==1: return print(coins_index_C[0],"th coin is the fake coin") else: A=[coins[i] for i in range(x,x+(y-x)//3+1)] B=[coins[i] for i in range(x+(y-x)//3+1,x+2*(y-x)//3+1)] C=[coins[i] for i in range(x+2*(y-x)//3+1,y)] coins_index_A=[i+1 for i in range(x,x+(y-x)//3+1)] coins_index_B=[i+1 for i in range(x+(y-x)//3+1,x+2*(y-x)//3+1)] coins_index_C=[i+1 for i in range(x+2*(y-x)//3+1,y)] if sum(A)<sum(B): if len(A)>1: testing_fake_one(x,x+(y-x)//3+1,(y-x)//3+1) if len(A)==1: return print(coins_index_A[0],"th coin is the fake coin") elif sum(A)>sum(B): if len(A)>1: testing_fake_one(x+(y-x)//3+1,x+2*(y-x)//3+1,(y-x)//3) if len(A)==1: return print(coins_index_B[0],"th coin is the fake coin") else: if len(A)>1: testing_fake_one(x+2*(y-x)//3+1,y,y-2*(y-x)//3-1-x) if len(A)==1: return print(coins_index_C[0],"th coin is the fake coin") n=int(input("Enter the number of coins:"))randomize_coins(n)testing_fake_one(0,n,n)

Time complexity for running this algorithm is O(log n), given, we assume that we can somehow find weight or sum in constant time.
Space complexity is O(log n), depending upon the number of coins.

Question

How much does splitting into 3 piles improve in performance on a decrease-by-half approach, as compared to the one in which we split the coins into two piles?

1.58

1.33

1.25

1.77

We need to find the ration of log n base 2 to log n base 3, which will become log 3 base 2 equaling 1.58.

With this article at OpenGenus, you must have the complete idea of Fake Coin Problem.

Fake Coin Problem (2024)

FAQs

What to do if you find a fake coin? ›

Surrender the note or coin only to a properly identified police officer or a U.S. Secret Service special agent.

How are fake coins detected? ›

NGC uses X-ray fluorescence spectroscopy, an extensive research catalog and other tools to determine a coin's authenticity. If deemed not genuine, the coin is not encapsulated. NGC offers numerous educational resources to help collectors and dealers avoid counterfeit or altered coins.

What is the fake coin problem in design and analysis of algorithms? ›

Counterfeit coins problem is the most important variant of these problems, where among a set of identical coins, the weight of some of them deviate from that of a standard coin, i.e. either heavier or lighter, and the objective is to recognize the forged coins by means of weighing.

What is the defective coin problem? ›

The defective coin problem involves the identification of a defective coin, if any, and ascertaining the nature of the defect (heavier/lighter) from a set of coins containing at the most one defective coin, using an equal‐arm‐pan‐balance.

Are fake coins illegal? ›

Counterfeiting -- 18 U.S.C. 489. Section 489 of Title 18 prohibits the making of any token, disc, or device in the likeness or similitude of coins in the United States, except under the authority of the Secretary of the Treasury.

Is it legal to own a counterfeit coin? ›

These laws make it a crime to: Hold, pass, publish, sell, or attempt, any counterfeit currency with the intent to defraud. Make, forge, or pass counterfeit foreign currency with the intent to defraud. Buy, transfer, receive, or deliver counterfeit currency with intent that it be passed off as a genuine currency.

What does a fake coin look like? ›

Compromised image sharpness and detail can sometimes indicate a fake. Specific edge characteristics can be particularly difficult to reproduce such as a ridged finish (reeding) or lettering. More obviously, weight and size of the coin can vary from the authentic version.

Are fake coins common? ›

Coin counterfeiting of valuable antique coins is common; modern high-value coins are also counterfeited and circulated. Counterfeit antique coins are generally made to a very high standard so that they can deceive experts.

Are replica coins worth anything? ›

Are coin replicas considered to be a good numismatic investment? The United States Mint does not comment on coin grading issues or on a replica's current or future value as a collectible item. If you like a replica because of the way it looks (e.g. magnified image), then you may want to add it to your collection.

What are fake coins called? ›

There are many different names used to describe counterfeit coins, some quite accurate, others euphemistic or colloquial. Commonly used names includes copy, forgery, restrike, reproduction and fake. The words fake, forgery and counterfeit are usually accurate, the others are often not.

Why do people make fake coins? ›

Fake Coin Types, & why people counterfeit coins

There are many coins that are so rare and prohibitively expensive that a normal collector would be unable to locate or acquire. So a collector may choose to add a copy or replica coin to a collection so as to have example of what a real one would look like.

How do you find fake coins with decrease and conquer? ›

The fake coin problem attempts to find one counterfit coin (weighing 1 gram less), among a pile of real coins. We use a scale and a Decrease-and-Conquer strategy to find the fake. One method is to recursivley divide the unknown pile into three equal piles until you find the fake coin.

What is the most common coin error? ›

A doubled die coin is one of the most common coin errors you can find. It's a coin struck twice by a die, resulting in two identical designs, slightly offset.

How common are coin errors? ›

Finding error coins is rare, but not impossible. Keep in mind that all of these error types tend to occur in batches of coins, as the U.S. Mint strikes coins for mass production. A die flaw or miss-strike will affect all of the coins from a particular production run.

Is a coin fake if its magnetic? ›

If a coin that is claimed to have a high gold or silver content is attracted to a magnet, then it is likely a counterfeit that contains more steel or iron than advertised. It's worth remembering that for this reason, some counterfeiters use other non-magnetic metals like copper and lead.

Do fake coins stick to magnets? ›

If it sticks to the magnet, then you know it is not real. Additional Tests While magnet testing is one quick test to eliminate fake coins/or fake bullion made with ferrous metals (iron, nickel, cobalt), there are additional tests that you may need to take to test your precious metals.

Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 5641

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.