Bit of a math question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bit of a math question
# 1  
Old 09-05-2014
Bit of a math question

I have a number, say 174. I need to write bash code that will find the first larger number that ends in 99. That would be 199 in this case. If the number were 1263, I would be looking for 1299, for 175438, I would want 175499, etc.

If the numbers were always three digit, I could just grab the first digit and add 99.
NEW_NUMBER=${OLD_NUMBER:0:1}'99'

I guess what I would want to do is to just remove the last two chars and replace with 99.
NEW_NUMBER=${OLD_NUMBER:0:-2}'99'

Does that look right??

LMHmedchem
# 2  
Old 09-05-2014
Longhand using OSX 10.7.5, default bash terminal...
Code:
Last login: Fri Sep  5 20:24:17 on ttys000
AMIGA:barrywalker~> x=1234567
AMIGA:barrywalker~> y=99
AMIGA:barrywalker~> z=${x:0:$((${#x}-2))}$y
AMIGA:barrywalker~> echo $z
1234599
AMIGA:barrywalker~> _

This User Gave Thanks to wisecracker For This Post:
# 3  
Old 09-05-2014
A minor nitpick - suppose your number is 123499. wisecrackers code will return 123499. Is that the NEXT larger number ending in 99, after 123499? You get to decide.

This kind of thing is sometimes called an edge condition.

BTW wisecracker, nice effort.
These 2 Users Gave Thanks to jim mcnamara For This Post:
# 4  
Old 09-05-2014
Try
Code:
num=174
echo $(((num/100+1)*100-1))

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 09-05-2014
scrutinizer's code - good but with the same problem it looks like....

What I am getting at is: problem as stated requires the addition of 1 to the starting number.

Crude example:
Code:
num=199
num=$(( $num + 1 ))
echo $(((num/100+1)*100-1))

# 6  
Old 09-05-2014
That could be remedied like this:
Code:
echo $((((num+1)/100+1)*100-1))

# 7  
Old 09-05-2014
@scrutinizer - absolutely, but I'm trying to get the OP to see your point.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies

2. Shell Programming and Scripting

Shell script for solving the math question

Can such Puzzle solve through UNIX script? if yes, what could be the code? This has been solve in C language. we were trying to solve this through shell but could not because of not able to pass 1st argument with multiple value. we are not expert in unix scripting. Below is the puzzle John is a... (4 Replies)
Discussion started by: anshu ranjan
4 Replies

3. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

4. Solaris

immutable bit question

Hi! All Just wondering if anyone has a idea about setting the immutable bit on a Solaris 10 ZFS file I tried this chmod S+ci toto.txt and got that :-( chmod: ERROR: invalid mode (0 Replies)
Discussion started by: Ex-Capsa
0 Replies

5. UNIX for Dummies Questions & Answers

Question regarding permision and seguid bit (sticky bit)

Hi , I am having file permision as drwxrwsr_x I kwo for deleting a file in the diretory i need w permsion as well .. Say if i am having the permsion as drwxrwsrwx - wil any one can delete the files in the directory .. And one more question what is the s doing there ..... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

6. UNIX for Dummies Questions & Answers

Simple Math question...

Hi All , I'm trying to do a simple math expression ...but unsuccessfull :-( Anyone can help me? days=23 amount=`expr ${days} / 30 \* -125` echo $amount but as result i got 0 when i expect 95.833333 Another question...how i can limit only to two or three decimal fields? Thanks in... (1 Reply)
Discussion started by: EDBGSK
1 Replies

7. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies

8. Shell Programming and Scripting

a math related question

hello: First I know the rules on Homework related questions, I wrote my script, but I cannot seem to figure out how to do one math problem. How do I take a zip code and seperate the idvidual digits? I used the modulus expression and divided the number by 10 ^ n but that only worked... (9 Replies)
Discussion started by: jahjah
9 Replies
Login or Register to Ask a Question