Sponsored Content
Operating Systems OS X (Apple) A Fun Perfect Square Checker Using Integer Arithmetic Only... ;o) Post 302952996 by disedorgue on Monday 24th of August 2015 08:22:47 PM
Old 08-24-2015
Hi,
For fun in bash:
Code:
#!/bin/bash

XX=$1
YY=$((${#XX}&1 ? 1 : 2))
ZZ=${XX:0:$YY}
AA=$(((${#XX}-YY)/2))
VV=${XX:$YY:$((AA*2))}
BB=0
DD=1
EE=1
while (($EE))
do
	ZZ=$((ZZ-DD))
	BB=$((ZZ>=0 ? BB+1 : BB))
	DD=$((ZZ>=0 ? DD+2 : DD-2))
	EE=$((ZZ>=0 ? 1 : 0))
	ZZ=$((ZZ>=0 ? ZZ : ZZ+DD+2))
done
rac=$rac$BB
II=0
while (($AA))
do
	BB=0
	EE=1
	DD=$((((DD+1)*10)+1))
	ZZ=$((ZZ*100))
	UU=${VV:$II:1}
	UU=$((UU*10))
	UU=$((UU+${VV:II+1:1}))
	ZZ=$((ZZ+UU))
	while (($EE))
	do
		ZZ=$((ZZ-DD))
		BB=$((ZZ>=0 ? BB+1 : BB))
		DD=$((ZZ>=0 ? DD+2 : DD-2))
		EE=$((ZZ>=0 ? 1 : 0))
		ZZ=$((ZZ>=0 ? ZZ : ZZ+DD+2))
	done
	rac=$rac$BB	
	II=$((II+2))
	AA=$((AA-1))
done
(( ! ZZ )) && echo $XX is perfect square of $rac || echo $XX is not a perfect square

Examples:
Code:
$ ./p_square.sh 391362529049165025
391362529049165025 is perfect square of 625589745
$ ./p_square.sh 98743978937000250000
98743978937000250000 is perfect square of 9937000500
$ ./p_square.sh 98743978937000250011
98743978937000250011 is not a perfect square

 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

extraction of perfect text from file.

Hi All, I have a file of the following format. <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user... (5 Replies)
Discussion started by: nua7
5 Replies

2. UNIX for Dummies Questions & Answers

A perfect number shell program

Here's my work of testing whether a number input is perfect or not.. echo Enter a number read no i=1 ans=0 while do if then ans='expr $ans + $i' fi i='expr $i + 1' done if then echo $no is perfect else echo $no is NOT perfect fi (12 Replies)
Discussion started by: Cyansnow
12 Replies

3. AIX

I want the perfect user-interface

I've got an aix-box somewhere on the network and a PC on my desk. Nothing fancy so far. The PC is made dual-boot: - windowsXP with putty & winSCP or - slackware 13 with xfce4 installed. The aix-box runs DB2 v8.2 and I've installed db2top to monitor the database. db2top is a character... (0 Replies)
Discussion started by: dr_te_z
0 Replies

4. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

5. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

6. UNIX for Dummies Questions & Answers

Can you perfect my sed ?

I want to print only the lines that meet the criteria : "worde:" and "wordo;" I got this far: sed -n '/\(*\)\1e:\1o;/p;' But it doesn't quite work. Can someone please perfect it and tell me exactly how its a fixed version/what was wrong with mine? Thanks heaps, (1 Reply)
Discussion started by: maximus73
1 Replies

7. Shell Programming and Scripting

egrep line with perfect mach

Hi Input File A L006 AL01 0 (OCK) L006 A006 0 (OCK) L011 AR11 1 (NLOCK) Input File B L006 AL01 0 (OCK) L006 A006 0 (OCK) Need Egrep Command for perfect Match Thanks (4 Replies)
Discussion started by: asavaliya
4 Replies

8. Shell Programming and Scripting

Not able to find the perfect code...Geting confused in between

I have to find last delimiter in each line of a file and store the value after the last '/' in a variable in ksh script...Pls Pls help me:(The file is as shown below: /opt/apps/cobqa/apps/abadv/bind/advc0007.bnd /opt/apps/cobqa/apps/abbrio/bind/naac6115.bnd... (5 Replies)
Discussion started by: bhavanabahety
5 Replies
mkgeo_grid(1rheolef)						    rheolef-6.1 					      mkgeo_grid(1rheolef)

NAME
mkgeo_grid -- build a strutured mesh of a parallelotope, in 1d, 2d or 3d SYNOPSIS
mkgeo_grid options [nx [ny [nz]]] EXAMPLE
The following command build a triangular based 2d 10x10 grid of the unit square: mkgeo_grid -t 10 > square-10.geo geo square-10.geo or in one comand line: mkgeo_grid -t 10 | geo - DESCRIPTION
This command is usefull when testing programs on simple geometries. It avoid the preparation of an input file for a mesh generator. The optional nx, ny and nz arguments are integer that specifies the subdivision in each direction. By default nx=10, ny=nx and nz=ny. The mesh files goes on standard output. The command supports all the possible element types: edges, triangles, rectangles, tetraedra, prisms and hexahedra. ELEMENT TYPE OPTIONS
-e 1d mesh using edges. -t 2d mesh using triangles. -q 2d mesh using quadrangles (rectangles). -T 3d mesh using tetraedra. -P 3d mesh using prisms. -H 3d mesh using hexahedra. THE GEOMETRY
The geometry can be any [a,b] segment, [a,b]x[c,d] rectangle or [a,b]x[c,d]x[f,g] parallelotope. By default a=c=f=0 and b=d=g=1, thus, the unit boxes are considered. For instance, the following command meshes the [-2,2]x[-1.5, 1.5] rectangle: mkgeo_grid -t 10 -a -2 -b 2 -c -1.5 -d 1.5 | geo - -a float -b float -c float -d float -f float -g float BOUNDARY DOMAINS
-sides -nosides The boundary sides are representd by domains: left, right, top, bottom,front and back. -boundary -noboundary This option defines a domain named boundary that groups all sides. By default, both sides and the whole boundary are defined as domains: mkgeo_grid -t 10 > square.geo geo square.geo mkgeo_grid -t 10 -nosides > square.geo geo square.geo mkgeo_grid -t 10 -noboundary > square.geo geo square.geo mkgeo_grid -t 10 -noboundary -nosides > square.geo geo square.geo REGIONS
-region -noregion The whole domain is splitted into two subdomains: east and west, This option is used for testing computations with subdomains (e.g. transmission problem; see the user manual). mkgeo_grid -t 10 -region | geo - CORNERS
-corner -nocorner The corners (four in 2D and eight in 3D) are defined as OD-domains. This could be usefull for some special boundary conditions. mkgeo_grid -t 10 -corner | geo - mkgeo_grid -T 5 -corner | geo - COORDINATE SYSTEM OPTION
Most of rheolef codes are coordinate-system independant. The coordinate system is specified in the geometry file `.geo'. -zr -rz the 2d mesh is axisymmetric: zr (resp. rz) stands when the symmetry is related to the first (resp. second) coordinate. FILE FORMAT OPTION
rheolef-6.1 rheolef-6.1 mkgeo_grid(1rheolef)
All times are GMT -4. The time now is 10:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy