Fermat number

Here given code implementation process.

//C Program
//Display of initial few fermat number
#include <stdio.h>

//Calculate power of number in given size
unsigned long long pow_number(int number,int size)
{

  if(number==0)
  {
    //when number is zero 
    return 0;
  }
  else if(size==0)
  {
    //Base Case when zero power
    return 1;
  }
  else
  {
    unsigned long long result=number;

    for (int i = 2; i <= size; ++i)
    {
      result=result*number;
    }
    return result;
  }
}
//Display n fermat number 
//Assume n between 1 - 6
void fermat_number(int n)
{
  
  for ( int i = 0; i < n; ++i)
  {
    //calculate and print 2^(2^i) 
    printf("%llu  ",pow_number(2,pow_number(2,i))+1 );

  }
}

int main()
{
 
  int n = 6;
  
  fermat_number(n);


  return 0;
}

Output

3  5  17  257  65537  4294967297
/*
 C++ Program
 Display of initial few fermat number
*/
#include<iostream>
using namespace std;

class MyNumber {
	public:

		//Calculate power of number in given size
		long pow_number(int number, long  size) {
			if (number == 0) {
				return 0;
			} else
			if (size == 0) {
				return 1;
			} else {
				long result = number;
				for (long i = 2; i <= size; ++i) {
					result = result *number;
				}
				return result;
			}
		}
	// Display n fermat number 
	// Assume n between 1 - 6 (result under int max size)
	void fermat_number(int n) {
		for (int i = 0; i < n; ++i) {
			//calculate and print 2^(2^i) 

			cout << " " << this->pow_number(2, this->pow_number(2, i)) + 1;
		}
	}
};
int main() {
	MyNumber obj ;
	obj.fermat_number(6);
	return 0;
}

Output

 3 5 17 257 65537 4294967297
/*
  Java Program
  Display of initial few fermat number
*/
public class MyNumber {
 

  //Calculate power of number in given size
  public long pow_number(int number,long size)
  {

    if(number==0)
    {
      //when number is zero 
      return 0;
    }
    else if(size==0)
    {
      //Base Case when zero power
      return 1;
    }
    else
    {
      long result=number;

      for (long i = 2; i <= size; ++i)
      {
        result=result*number;
      }
      return result;
    }
  }
  // Display n fermat number 
  // Assume n between 1 - 6 (result under int max size)
  public void fermat_number(int n)
  {
    
    for ( int i = 0; i < n; ++i)
    {
      //calculate and print 2^(2^i) 
     
      System.out.print("  "+(pow_number(2,pow_number(2,i))+1) );

    }
  }

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    obj.fermat_number(6);
  }
}

Output

 3 5 17 257 65537 4294967297
/*
  C# Program
  Display of initial few fermat number
*/
using System;
public class MyNumber {


	//Calculate power of number in given size
	public long pow_number(int number, long size) {

		if (number == 0) {
			//when number is zero 
			return 0;
		} else if (size == 0) {
			//Base Case when zero power
			return 1;
		} else {
			long result = number;

			for (long i = 2; i <= size; ++i) {
				result = result * number;
			}
			return result;
		}
	}
	// Display n fermat number 
	// Assume n between 1 - 6 (result under int max size)
	public void fermat_number(int n) {

		for (int i = 0; i < n; ++i) {
			//calculate and print 2^(2^i) 

			Console.Write("  " + (pow_number(2, pow_number(2, i)) + 1));

		}
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		obj.fermat_number(6);
	}
}

Output

  3  5  17  257  65537  4294967297
<?php
/*
  Php Program
  Display of initial few fermat number
*/
class MyNumber {
	//Calculate power of number in given size

	public 	function pow_number($number, $size) {
		if ($number == 0) {
			return 0;
		} else
		if ($size == 0) {
			return 1;
		} else {
			$result = $number;
			for ($i = 2; $i <= $size; ++$i) {
				$result = $result *$number;
			}
			return $result;
		}
	}
	// Display n fermat number 
	// Assume n between 1 - 7 (result under int max size)

	public 	function fermat_number($n) {
		for ($i = 0; $i < $n; ++$i) {
			//calculate and print 2^(2^i) 

			echo(" ". ($this->pow_number(2, $this->pow_number(2, $i)) + 1));
		}
	}
};

function main() {
	$obj = new MyNumber();
	$obj->fermat_number(6);
}
main();

Output

 3 5 17 257 65537 4294967297
/*
 Node Js Program
 Display of initial few fermat number
*/
class MyNumber {
	//Calculate power of number in given size
	pow_number(number, size) {
		if (number == 0) {
			return 0;
		} else
		if (size == 0) {
			return 1;
		} else {
			var result = number;
			for (var i = 2; i <= size; ++i) {
				result = result *number;
			}
			return result;
		}
	}
	// Display n fermat number 
	// Assume n between 1 - 6 (result under int max size)
	fermat_number(n) {
		for (var i = 0; i < n; ++i) {
			//calculate and print 2^(2^i) 

			process.stdout.write(" " + (this.pow_number(2, this.pow_number(2, i)) + 1));
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	obj.fermat_number(6);
}
main();

Output

 3 5 17 257 65537 4294967297
# Python 3 Program
# Display of initial few fermat number
class MyNumber :
	# Calculate power of number in given size
	def pow_number(self, number, size) :
		if (number == 0) :
			return 0
		elif (size == 0) :
			return 1
		else :
			result = number
			i = 2
			while (i <= size) :
				result = result * number
				i += 1
			
			return result
		
	
	#  Display n fermat number 
	#  Assume n between 1 - 6 (result under int max size)
	def fermat_number(self, n) :
		i = 0
		while (i < n) :
			# calculate and print 2^(2^i) 
			print(" ", (self.pow_number(2, self.pow_number(2, i)) + 1),end="")
			i+=1
		
	

def main() :
	obj = MyNumber()
	obj.fermat_number(6)


if __name__ == "__main__":
	main()

Output

 3 5 17 257 65537 4294967297
# Ruby Program 
# Display of initial few fermat number
class MyNumber 
	# Calculate power of number in given size
	def pow_number(number, size) 
		if (number == 0) 
			return 0
		elsif (size == 0) 
			return 1
		else 
			result = number
			i = 2
			while (i <= size) 
				result = result * number
				i += 1
			end
			return result
		end
	end
	#  Display n fermat number 
	#  Assume n between 1 - 6 (result under int max size)
	def fermat_number(n) 
		i = 0
		while (i < n) 
			# calculate and print 2^(2^i) 

			print(" ", (self.pow_number(2, self.pow_number(2, i)) + 1))
			i+=1
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.fermat_number(6)
end
main()

Output

 3 5 17 257 65537 4294967297
/*
 Scala Program
 Display of initial few fermat number
*/
class MyNumber {
	//Calculate power of number in given size
	def pow_number(number: Int, size: Long): Long = {
		if (number == 0) {
			return 0;
		} else
		if (size == 0) {
			return 1;
		} else {
			var result: Long = number;
			var i: Long = 2;
			while (i <= size) {
				result = result * number;
				i += 1;
			}
			return result;
		}
	}
	// Display n fermat number 
	// Assume n between 1 - 6 (result under int max size)
	def fermat_number(n: Int): Unit = {
		var i: Int = 0;
		while (i < n) {
			//calculate and print 2^(2^i)
			print(" " + (this.pow_number(2, this.pow_number(2, i)) + 1));
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
  		obj.fermat_number(6);
	}
}

Output

 3 5 17 257 65537 4294967297
/*
  Swift 4 Program
  Display of initial few fermat number
*/
class MyNumber {
	//Calculate power of number in given size
	func pow_number(_ number: Int, _ size: Int) -> Int {
		if (number == 0) {
			return 0;
		} else
		if (size == 0) {
			return 1;
		} else {
			var result: Int = number;
			var i: Int = 2;
			while (i <= size) {
				result = result * number;
				i += 1;
			}
			return result;
		}
	}
	// Display n fermat number 
	// Assume n between 1 - 6 (result under int max size)
	func fermat_number(_ n: Int) {
		var i: Int = 0;
		while (i < n) {
			//calculate and print 2^(2^i)
			print(" ", (self.pow_number(2, self.pow_number(2, i)) + 1),terminator:"");
			i += 1;
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.fermat_number(6);
}
main();

Output

  3  5  17  257  65537  4294967297


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved