LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2024, 04:43 PM   #1
blumenwesen
Member
 
Registered: Jan 2022
Posts: 58

Rep: Reputation: 0
help factor calculation in bash?


hello, I have tried the factors with a comprehensible calculation method without "factor..." to calculate, the loop still works with 55 = 5 11 but at 120 = 30 4.
i have lost track, can someone help me further?

Code:
z=120; y=$z; x=$z; 
until (( $[y--] < 3 )); do 
until (( $x <= $y || $[$x/$y] <= $y || $[(${x}0/$y)%10] != 0 )); do 
x=$[$x/$y]; 
echo $x $y $[$x/$y]
done; done; 
echo -e "\n"$[$z/$x] $x" \n"
factor 120 # correct result = 120: 2 2 2 3 5

Last edited by blumenwesen; 08-11-2024 at 04:45 PM.
 
Old 08-11-2024, 05:17 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,112

Rep: Reputation: 6062Reputation: 6062Reputation: 6062Reputation: 6062Reputation: 6062Reputation: 6062Reputation: 6062Reputation: 6062Reputation: 6062Reputation: 6062Reputation: 6062
I read past the comprehensible calculation method... standby

Last edited by michaelk; 08-11-2024 at 05:36 PM.
 
Old 08-12-2024, 03:45 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,639

Rep: Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525
you can add set -xv to see how does it work. You can add some log lines, like print $x $y whatever to understand better what's going on.
I don't really understand how would your code print "2 2 2 3 5" at all.
 
Old 08-12-2024, 04:21 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,915

Rep: Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236
Instead of the oldish $[ ] get used to $(( )) that is like (( )) but echos a result.
Code:
x=$(($x/$y))
or
Code:
x=$((x / y))
or
Code:
((x /= y))
I did not debug your code. The following is a working code.
Code:
#!/bin/bash
x=${1:-120}
# Extra handling of divisor 2
until ((x % 2))
do
  echo 2
  ((x /= 2))
done
for ((y = 3; y <= x / y; y += 2))
do
  until ((x % y))
  do
    echo $y
    ((x /= y))
  done
done
((x > 2)) &&
  echo $x

Last edited by MadeInGermany; 08-12-2024 at 05:50 AM. Reason: Using y <= x / y
 
Old 08-12-2024, 04:27 AM   #5
marijan
LQ Newbie
 
Registered: Aug 2013
Posts: 14

Rep: Reputation: Disabled
Factor numbers in bourne shell
Code:
z=120
d=2
while [ $(($d*$d)) -le $z ]; do
  while [ $(($z%$d)) -eq 0 ]; do
    echo -n $d ""
    z=$(($z/$d))
  done
  d=$(($d+1))
done
if [ $z -ne 1 ]; then
  echo -n $z
fi
echo

Last edited by marijan; 08-12-2024 at 04:41 AM.
 
Old 08-12-2024, 12:46 PM   #6
blumenwesen
Member
 
Registered: Jan 2022
Posts: 58

Original Poster
Rep: Reputation: 0
Wow, sorry I only had time to reply now.
Thanks for the help.
Now I come on with my experiment, a run of several primes decomposed into factors by permutation in a superordinate space or "cube" taking each position one after the other. =D

Last edited by blumenwesen; 08-12-2024 at 12:55 PM.
 
Old 08-13-2024, 12:11 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,639

Rep: Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525
Quote:
Originally Posted by blumenwesen View Post
Wow, sorry I only had time to reply now.
Thanks for the help.
Now I come on with my experiment, a run of several primes decomposed into factors by permutation in a superordinate space or "cube" taking each position one after the other. =D
I don't really understand, just wanted to mention: bash is not really suitable for this, if you need higher speed you need to use another language.
 
Old 09-08-2024, 10:23 AM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,325

Rep: Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919
recated too prīmz:
https://www.linuxquestions.org/quest...ys-4175739404/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Discussion: Multi-factor and two-factor authentication richinsc Linux - Security 7 09-22-2011 01:29 AM
[SOLVED] BASH: Download Script -> While Loop Calculation Toubles... Devcon Programming 5 07-29-2010 01:41 AM
Simple bash calculation Hiperi0n Programming 12 11-14-2008 08:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration