Consider a series of numbers composed of only digits 2, 3, 5, 7 (primes). First few numbers in the series are 2, 3, 5, 7, 22, 23, 25, 27, 32, 33, 35, 37, 52, 53, 55, 57 .. etc. Given a number constructed by 2, 3, 5, 7 digit only, we need to find position of this number in this series.
Examples:
Input : 22
Output : 5
22 is 5th number in series 2, 3, 5, 7, 22, ...
Input : 777
Output : 84
It is reverse of the following article :
Finding n-th number made of prime digits (2, 3, 5 and 7) only
""
/ | | \
1(2) 2(3) 3(5) 4(7)
/ | | \ / | | \ / | | \ / | | \
5(22) 6(23) 7(25) 8(27) 9(32)10(33)11(35)12(37) 13(52) 14(53) 15(55) 16(57) 17(72) 18(73) 19(75) 20(77)
/||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\ /||\
If number is 2 then it is on the position pos*2+1
If number is 3 then it is on the position pos*2+2
If number is 5 then it is on the position pos*2+3
If number is 7 then it is on the position pos*2+4
Here pos is an integer greater than or equal to 0.
C++
#include <algorithm>
#include <iostream>
using namespace std;
int findpos(string n)
{
int pos = 0;
for ( int i = 0; n[i] != '\0' ; i++) {
switch (n[i]) {
case '2' :
pos = pos * 4 + 1;
break ;
case '3' :
pos = pos * 4 + 2;
break ;
case '5' :
pos = pos * 4 + 3;
break ;
case '7' :
pos = pos * 4 + 4;
break ;
}
}
return pos;
}
int main()
{
string n = "777" ;
cout << findpos(n);
}
|
Java
import java.io.*;
public class GFG
{
static int findpos(String n)
{
int pos = 0 ;
for ( int i = 0 ; i < n.length(); i++)
{
switch (n.charAt(i))
{
case '2' :
pos = pos * 4 + 1 ;
break ;
case '3' :
pos = pos * 4 + 2 ;
break ;
case '5' :
pos = pos * 4 + 3 ;
break ;
case '7' :
pos = pos * 4 + 4 ;
break ;
}
}
return pos;
}
public static void main(String args[])
{
String n = "777" ;
System.out.println( findpos(n));
}
}
|
Python 3
def findpos(n):
pos = 0
for i in n:
if i = = '2' :
pos = pos * 4 + 1
elif i = = '3' :
pos = pos * 4 + 2
elif i = = '5' :
pos = pos * 4 + 3
elif i = = '7' :
pos = pos * 4 + 4
return pos
n = "777"
print (findpos(n))
|
C#
using System;
class GFG
{
static int findpos(String n)
{
int pos = 0;
for ( int i = 0; i < n.Length; i++)
{
switch (n[i])
{
case '2' :
pos = pos * 4 + 1;
break ;
case '3' :
pos = pos * 4 + 2;
break ;
case '5' :
pos = pos * 4 + 3;
break ;
case '7' :
pos = pos * 4 + 4;
break ;
}
}
return pos;
}
public static void Main(String[] args)
{
String n = "777" ;
Console.WriteLine( findpos(n));
}
}
|
PHP
<?php
function findpos( $n )
{
$pos = 0;
for ( $i = 0; isset( $n [ $i ]) != NULL; $i ++)
{
switch ( $n [ $i ])
{
case '2' :
$pos = $pos * 4 + 1;
break ;
case '3' :
$pos = $pos * 4 + 2;
break ;
case '5' :
$pos = $pos * 4 + 3;
break ;
case '7' :
$pos = $pos * 4 + 4;
break ;
}
}
return $pos ;
}
$n = "777" ;
echo findpos( $n );
?>
|
Javascript
<script>
function findpos(n)
{
var pos = 0;
for (i = 0; i < n.length; i++)
{
switch (n.charAt(i))
{
case '2' :
pos = pos * 4 + 1;
break ;
case '3' :
pos = pos * 4 + 2;
break ;
case '5' :
pos = pos * 4 + 3;
break ;
case '7' :
pos = pos * 4 + 4;
break ;
}
}
return pos;
}
var n = "777" ;
document.write( findpos(n));
</script>
|